Spring MVC上传、下载 文件
1,上传文件
public static String upload(MultipartFile file, SysUserBean sysUserBean, HttpServletRequest request){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = new Date();
String path = request.getSession().getServletContext().getRealPath("upload\\"+sysUserBean.getId()+"\\"+sdf.format(date));
String fileLastName = "";
String fileName = "";
if(file != null){
int dot = file.getOriginalFilename().lastIndexOf('.');
if(dot>0){
fileLastName = file.getOriginalFilename().substring(dot);
fileName = UUID.randomUUID()+fileLastName;
File targetFile = new File(path, fileName);
if(!targetFile.exists()){
targetFile.mkdirs();
}
try {
file.transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
return "";
}
return "upload/"+sysUserBean.getId()+"/"+sdf.format(date)+"/"+fileName;
}else{
return "";
}
}
return "";
}
2,下载文件
public static void download(String fileName, HttpServletRequest request,
HttpServletResponse response)throws Exception {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("UTF-8");
java.io.BufferedInputStream bis = null;
java.io.BufferedOutputStream bos = null; String path = request.getSession().getServletContext().getRealPath("\\");
String downLoadPath = path + fileName;
System.out.println(downLoadPath);
try {
long fileLength = new File(downLoadPath).length();
response.setContentType("application/x-msdownload;");
response.setHeader("Content-disposition", "attachment; filename="
+ new String(fileName.getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(fileLength));
bis = new BufferedInputStream(new FileInputStream(downLoadPath));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
} }
3,base64 转成图片
// 对字节数组字符串进行Base64解码并生成图片
public static String GenerateImage(String imgStr,HttpServletRequest request,String userid) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = new Date();
if (imgStr == null) // 图像数据为空
return "";
UUID uuid = UUID.randomUUID();
String fileName = uuid+".jpg";
String path = request.getSession().getServletContext().getRealPath("upload\\"+userid+"\\"+sdf.format(date));
File targetFile = new File(path);
path = path+"\\"+fileName;
System.out.println(path);
if(!targetFile.exists()){
targetFile.mkdirs();
}
BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64解码
byte[] bytes = decoder.decodeBuffer(imgStr);
for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {// 调整异常数据
bytes[i] += 256;
}
}
// 生成jpeg图片
OutputStream out = new FileOutputStream(path);
out.write(bytes);
out.flush();
out.close();
return "upload/"+userid+"/"+sdf.format(date)+"/"+fileName;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
Spring MVC上传、下载 文件的更多相关文章
- spring mvc上传下载文件
前端jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...
- Spring框架学习(8)spring mvc上传下载
内容源自:spring mvc上传下载 如下示例: 页面: web.xml: <?xml version="1.0" encoding="UTF-8"?& ...
- asp.net mvc 上传下载文件的几种方式
view: <!DOCTYPE html> <html> <head> <meta name="viewport" content=&qu ...
- Spring MVC上传文件原理和resolveLazily说明
问题:使用Spring MVC上传大文件,发现从页面提交,到进入后台controller,时间很长.怀疑是文件上传完成后,才进入.由于在HTTP首部自定义了“Token”字段用于权限校验,Token的 ...
- Spring MVC上传文件
Spring MVC上传文件 1.Web.xml中加入 <servlet> <servlet-name>springmvc</servlet-name> <s ...
- Spring MVC 上传文件
Spring MVC上传文件需要如下步骤: 1.前台页面,form属性 method设置为post,enctype="multipart/form-data" input的typ ...
- rz和sz上传下载文件工具lrzsz
######################### rz和sz上传下载文件工具lrzsz ####################################################### ...
- linux上很方便的上传下载文件工具rz和sz
linux上很方便的上传下载文件工具rz和sz(本文适合linux入门的朋友) ##########################################################&l ...
- shell通过ftp实现上传/下载文件
直接代码,shell文件名为testFtptool.sh: #!/bin/bash ########################################################## ...
- .Net mvc 上传多文件
.net mvc 上传多文件有很多种方式,我的方法只是其中一种, 仅供参考,我主要是注重参数传递的过程,后面文件保存的地方省略.. 调试环境 vs2017 控制器代码: [HttpPost] publ ...
随机推荐
- hibernate(一对多关系)
代码 public class Main { public static void main(String[] args) { SessionFactory sty = HibernateUtil ...
- nextJS使用注意事项
项目参考 nextJs-yicha 1. 采用方案 create-next-app.antd (1)安装 npx create-next-app --example with-ant-design m ...
- P1655 小朋友的球
P1655 小朋友的球 题目描述 @发源于 小朋友最近特别喜欢球.有一天他脑子抽了,从口袋里拿出了N个不同的球,想把它们放到M个相同的盒子里,并且要求每个盒子中至少要有一个球,他好奇有几种放法,于是尝 ...
- sql 生成javabean实体
select a.name,c.name,b.name,'private String '+lower(c.name)+';' from sysobjects a, systypes b, sysco ...
- SecureCRT是最常用的终端仿真程序,简单的说就是Windows下登录UNIX或Liunx服务器主机的软件,本文主要介绍SecureCRT的使用方法和技巧
SecureCRT是最常用的终端仿真程序,简单的说就是Windows下登录UNIX或Liunx服务器主机的软件,本文主要介绍SecureCRT的使用方法和技巧 VanDyke CRT 和 VanDyk ...
- (转)在Source Insight中看Python代码
http://blog.csdn.net/lvming404/archive/2009/03/18/4000394.aspx SI是个很强大的代码查看修改工具,以前用来看C,C++都是相当happy的 ...
- shell 单引号&双引号的使用
使用双引号: shell> X='parameter' shell> echo "Hello $X" Hello parameter 单引号中嵌套单引号: shell& ...
- 如何理解CUDA中的cudaMalloc()的参数
首先看下此运行时函数的原型: cudaError_t cudaMalloc (void **devPtr, size_t size ); 主要的第一个参数.为什么是两个星星呢?用个例子来说明下. fl ...
- mysql分区管理语句
1.key分区语句: ALTER TABLE order_info PARTITION BY KEY(orderSn) PARTITIONS 127; 2.rang分区语句: ALTER TABLE ...
- eclipse新建maven项目和聚合项目
1.new maven project : next 2.勾选 create a simple project : next 3.Group Id:项目的包路径 如com.jiayou.zjl, ...