(上传)
1.jar包与springmvc.xml
将request包装成MultipartHttpServletRequest.
2.页面
注意method为post
3.action
方法一解析(request)
方法二(参数)
(下载)
- @RequestMapping("/download/{fileName}")
- public ModelAndView download(@PathVariable("fileName")
- 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 ctxPath = request.getSession().getServletContext().getRealPath(
- "/")
- + "\\" + "images\\";
- String downLoadPath = ctxPath + 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();
- }
- return null;
- }
- }
时间: 2024-10-12 10:37:17