@RequestMapping("/downLoadFile") public void DownLoadFile(HttpServletRequest request, HttpServletResponse response) throws IOException { String fileName=request.getParameter("fileName"); response.setContentType("text/html;charset=utf-8"); response.setCharacterEncoding("utf-8"); //System.out.println(fileName); //FileInputStream in = new FileInputStream("G:\\"+fileName); FileInputStream in = new FileInputStream("/"+fileName);//static目錄下 response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("fileName", "utf-8")); OutputStream out = response.getOutputStream(); // 建立緩衝區 byte buffer[] = new byte[1024]; int len = 0; // 循環將輸入流中的內容讀取到緩衝區中 while ((len = in.read(buffer)) > 0) { // 輸出緩衝區內容到瀏覽器,實現文件下載 out.write(buffer, 0, len); } // 關閉文件流 in.close(); // 關閉輸出流 out.close(); } @RequestMapping(value = "/Test") public String Test(HttpServletRequest request, HttpServletResponse response) throws IOException { System.out.println("請求成功"); System.out.println(GetInputStreamString(request)); return "請求成功"; } public static String GetInputStreamString(HttpServletRequest request) throws IOException { StringBuffer data = new StringBuffer(); String line = null; BufferedReader reader = request.getReader(); while ((line = reader.readLine()) != null) data.append(line); return data.toString(); } @RequestMapping(value = "/shangchuan") public String GetFile(HttpServletRequest request, HttpServletResponse response) throws IOException { ServletInputStream sis = request.getInputStream(); String path=ClassUtils.getDefaultClassLoader().getResource("").getPath();//獲取項目的絕對路徑只會存到target中 FileOutputStream fos = new FileOutputStream(path+"static/File/123.jpg"); byte[] media = new byte[1024]; int length = sis.read(media, 0, 1024); while(length != -1) { fos.write(media, 0, length); length = sis.read(media, 0, 1024); } fos.close(); sis.close(); return "上傳成功"; }