Spring MVC上传、下载 文件
1,上传文件
1 public static String upload(MultipartFile file, SysUserBean sysUserBean, HttpServletRequest request){ 2 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); 3 Date date = new Date(); 4 String path = request.getSession().getServletContext().getRealPath("upload\\"+sysUserBean.getId()+"\\"+sdf.format(date)); 5 String fileLastName = ""; 6 String fileName = ""; 7 if(file != null){ 8 int dot = file.getOriginalFilename().lastIndexOf('.'); 9 if(dot>0){ 10 fileLastName = file.getOriginalFilename().substring(dot); 11 fileName = UUID.randomUUID()+fileLastName; 12 File targetFile = new File(path, fileName); 13 if(!targetFile.exists()){ 14 targetFile.mkdirs(); 15 } 16 try { 17 file.transferTo(targetFile); 18 } catch (Exception e) { 19 e.printStackTrace(); 20 return ""; 21 } 22 return "upload/"+sysUserBean.getId()+"/"+sdf.format(date)+"/"+fileName; 23 }else{ 24 return ""; 25 } 26 } 27 return ""; 28 }
2,下载文件
1 public static void download(String fileName, HttpServletRequest request, 2 HttpServletResponse response)throws Exception { 3 response.setContentType("text/html;charset=utf-8"); 4 request.setCharacterEncoding("UTF-8"); 5 java.io.BufferedInputStream bis = null; 6 java.io.BufferedOutputStream bos = null; 7 8 String path = request.getSession().getServletContext().getRealPath("\\"); 9 String downLoadPath = path + fileName; 10 System.out.println(downLoadPath); 11 try { 12 long fileLength = new File(downLoadPath).length(); 13 response.setContentType("application/x-msdownload;"); 14 response.setHeader("Content-disposition", "attachment; filename=" 15 + new String(fileName.getBytes("utf-8"), "ISO8859-1")); 16 response.setHeader("Content-Length", String.valueOf(fileLength)); 17 bis = new BufferedInputStream(new FileInputStream(downLoadPath)); 18 bos = new BufferedOutputStream(response.getOutputStream()); 19 byte[] buff = new byte[2048]; 20 int bytesRead; 21 while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { 22 bos.write(buff, 0, bytesRead); 23 } 24 } catch (Exception e) { 25 e.printStackTrace(); 26 } finally { 27 if (bis != null) 28 bis.close(); 29 if (bos != null) 30 bos.close(); 31 } 32 }
3,base64 转成图片
1 // 对字节数组字符串进行Base64解码并生成图片 2 public static String GenerateImage(String imgStr,HttpServletRequest request,String userid) { 3 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); 4 Date date = new Date(); 5 if (imgStr == null) // 图像数据为空 6 return ""; 7 UUID uuid = UUID.randomUUID(); 8 String fileName = uuid+".jpg"; 9 String path = request.getSession().getServletContext().getRealPath("upload\\"+userid+"\\"+sdf.format(date)); 10 File targetFile = new File(path); 11 path = path+"\\"+fileName; 12 System.out.println(path); 13 if(!targetFile.exists()){ 14 targetFile.mkdirs(); 15 } 16 BASE64Decoder decoder = new BASE64Decoder(); 17 try { 18 // Base64解码 19 byte[] bytes = decoder.decodeBuffer(imgStr); 20 for (int i = 0; i < bytes.length; ++i) { 21 if (bytes[i] < 0) {// 调整异常数据 22 bytes[i] += 256; 23 } 24 } 25 // 生成jpeg图片 26 OutputStream out = new FileOutputStream(path); 27 out.write(bytes); 28 out.flush(); 29 out.close(); 30 return "upload/"+userid+"/"+sdf.format(date)+"/"+fileName; 31 } catch (Exception e) { 32 e.printStackTrace(); 33 return ""; 34 } 35 }