package com.zt.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.util.Date; import java.util.List; @Controller @RequestMapping(value = "/uploadfile", method = RequestMethod.POST) public class UploadFileController { @RequestMapping("/uploadfile") //这个代码会在本地生成一个文件(单个文件上传到本地) 单文件上传 public String testUploadRequest(HttpServletRequest request, MultipartHttpServletRequest multiReq) { //获取文件上传的路径 String Uploadfilepath = multiReq.getFile("file1").getOriginalFilename(); System.out.println("Uploadfilepath==" + Uploadfilepath); //截取上传的文件名 String filename = Uploadfilepath.substring(Uploadfilepath.lastIndexOf('\\') + 1, Uploadfilepath.indexOf('.')); System.out.println("filename==" + filename); //截取上传文件的后缀 String fileSuffix = Uploadfilepath.substring(Uploadfilepath.indexOf('.') + 1, Uploadfilepath.length()); System.out.println("fileSuffix==" + fileSuffix); FileOutputStream fos = null;//文件输入流 FileInputStream fis = null; try { fis = (FileInputStream) multiReq.getFile("file1").getInputStream(); //类型转换 字符输入流转换为文件输入流 fos = new FileOutputStream(new File("E:\\" + filename + ".") + fileSuffix); byte[] temp = new byte[1024]; int len = fis.read(temp); while (len != -1) { fos.write(temp, 0, temp.length); fos.flush(); len = fis.read(temp); } } catch (IOException e) { e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } System.out.println("成功!!!"); String msg = "成功"; return "upload"; } @RequestMapping("/uploadfiles") // @ResponseBody 多文件上传 public String testUploadRequests(HttpServletRequest request) { List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file1"); MultipartFile file = null; BufferedOutputStream stream = null;//缓冲流 BufferedInputStream inputStream=null; for (int i = 0; i < files.size(); i++) { file = files.get(i); if (!file.isEmpty()) { try { String filepath = file.getOriginalFilename(); System.out.println("文件路径==" + filepath); //截取上传的文件名 String uploadfilename = filepath.substring(filepath.lastIndexOf('\\') + 1, filepath.indexOf('.')); System.out.println("文件名==" + uploadfilename); //获取文件的后缀名 String filesuffix = filepath.substring(filepath.indexOf('.') + 1, filepath.length()); System.out.println("后缀名==" + filesuffix); stream = new BufferedOutputStream(new FileOutputStream(new File("E:\\" + uploadfilename + "." + filesuffix))); byte[] bytes = new byte[1024]; int len=inputStream.read(bytes); if(len!=-1) { stream.write(bytes, 0, bytes.length); } } catch (Exception e) { e.printStackTrace(); } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { e.printStackTrace(); } } } } else { System.out.println("上传文件为空"); } } System.out.println("文件上传成功"); return "upload"; } //文件下载 @RequestMapping(value = "/filedown" , method = RequestMethod.GET) public void Testdown(HttpServletResponse res) { Date date=new Date(); System.out.println("date=="+date); String filename = ""; res.setHeader("content-type","application/octet-stream"); res.setContentType("application/octet-stream"); res.setHeader("Content-Disposition","attachment;filename="+filename); byte[] byts = new byte[1024]; BufferedInputStream bis=null; OutputStream os = null; try { os = res.getOutputStream(); bis = new BufferedInputStream(new FileInputStream(new File("E:\\down\\111.png"+filename)));//此处直接写死路径用于下载 int i=bis.read(byts); while (i != -1){ os.write(byts,0,byts.length); os.flush(); i = bis.read(byts); } } catch (IOException e) { e.printStackTrace(); }finally { if(bis != null){ try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if(os != null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } } }