Spring MVC上传、下载 文件

时间:2022-01-23 14:37:06

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 "";
}
}