10-springmvc-文件上传下载
发布日期:2021-05-10 18:27:57 浏览次数:19 分类:精选文章

本文共 4552 字,大约阅读时间需要 15 分钟。

Spring MVC ���������������������������������������

������������������������Spring MVC������������������������������������������������������Commons IO���FileUpload������������Spring MVC���MultipartResolver���������������

���������������������������

���������������������������������������������������

commons-io
commons-io
2.6
commons-fileupload
commons-fileupload
1.4

���������������������������������������������������������������

������������������������

���������������������������������

  • HTML������������
    ������Spring MVC���\Annotation-config���������������������������������������������������������
    1. Spring MVC Controller������
      ������������������������������������������������������������������������������Spring MVC Upload Controller���
    2. @Controller
      @RequestMapping("/file-upload")
      public class File Upload Controller {
      @Autowired
      private CommonsMultipartResolver multipartResolver;
      @RequestMapping(method = RequestMethod.POST)
      public String uploadFile(@RequestParam(value = "file-name") String fileName,
      @ultipart @RequestParam(name = "file-uploader") MultipartFile file,
      HttpSession session) {
      String originalFileName = file.getOriginalFilename();
      long fileSize = file.getSize();
      // ������������������������
      String uploadPath = session.getServletContext().getRealPath("/upload/");
      try {
      file.transferTo(new FileOutputStream(uploadPath + fileName));
      } catch (IOException e) {
      e.printStackTrace();
      }
      return "������������������" + originalFileName;
      }
      }
      1. ���������������������������
        ������������������������������������������������������������������������������������������
      2. public class File Size Interceptor implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        int maxSize = 1024 * 1024; // 1MB
        long contentLength = request.getContentLength();
        if (contentLength > maxSize) {
        throw new RuntimeException("������������������������������");
        }
        return true;
        }
        }

        ���������Spring MVC���������������������������������������

        ������������������������

        ������������������������������������������������������������������

      3. HTTP���������������
        ���������������������Content-Disposition������������������������������������������������������
      4. @Controller
        @RequestMapping("/download")
        public class File Download Controller {
        @Autowired
        private HttpSession httpSession;
        @RequestMapping(method = RequestMethod.GET)
        public void downFile(@RequestParam(name = "file-name") String fileName,
        HttpSession httpSession,
        HttpServletResponse response) {
        String filePath = httpSession.getServletContext().getRealPath("/download/");
        Path path = Paths.get(filePath + fileName);
        // ���������������������
        try (FileInputStream fileInputStream = new FileInputStream(path.toFile())) {
        response.setHeader("Content-Disposition", "attachment; filename\"" + fileName + "\"");
        CommonsIoUtils.copy(fileInputStream, response.getOutputStream());
        } catch (IOException e) {
        e.printStackTrace();
        }
        }
        }
        1. ������������������������������
          ������Commons IO���copy���������������������������������
        2. IOUtils.copy(new FileInputStream(path), response.getOutputStream());

          ������Spring MVC������

          ���Spring MVC������������������������������������������������

          ������������������

        3. ������������������������

          ������������������������������������������������������������������������������������

        4. ���������������������

          ���������������������maxInMemorySize���maxUploadSize���������������������������������������������������

        5. ������������

          ������������������������������������������������������������������������������������

        6. ���������������

          ������������������������IP���������������������������������������������������������������������������

        7. ���������������������������������������Spring MVC������������������������������������������������������������������������������������������������������������������������������������������������������������������Commons IO���FileUpload���������������������������

    上一篇:11-springmvc-验证码
    下一篇:09-springmvc-拦截器

    发表评论

    最新留言

    路过,博主的博客真漂亮。。
    [***.116.15.85]2025年04月06日 09时48分21秒