springmvc文件上传和文件下载

1.首先springMVC的配置文件:

<!-- 配置 MultipartResolver 文件上传-->
     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
         <property name="defaultEncoding" value="UTF-8"/>
         <property name="maxUploadSize" value="1024000000"/>
     </bean>
    <!-- SpringMVC在超出上传文件限制时,会抛出org.springframework.web.multipart.MaxUploadSizeExceededException -->
    <!-- 该异常是SpringMVC在检查上传的文件信息时抛出来的,而且此时还没有进入到Controller方法中 -->
    <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <!-- 遇到MaxUploadSizeExceededException异常时,自动跳转到/WEB-INF/jsp/error_fileupload.jsp页面 -->
                <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error_fileupload</prop>
            </props>
        </property>
    </bean>

导需要的入包

 <!-- commons包 -->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.2</version>
    </dependency>

    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.4</version>
    </dependency>

2.前端页面

<form name="form4" action="/fileupload" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" value="upload"/>
</form>

3.后端接收代码:

/**
     * 上传文件
     */
    @RequestMapping(value = "/fileupload",method = RequestMethod.POST)
    public ModelAndView fileUpload(@RequestParam(value = "file") MultipartFile multipartFile) throws IOException {
        String tempPath = "d://temp" + File.separator;
        File dir = new File(tempPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        String storageFilePath = null;//文件路径
        String originalFilename = null;//原文件
        if (multipartFile.isEmpty()) {
            throw new RuntimeException("文件不存在");
        } else {
            originalFilename = multipartFile.getOriginalFilename();
            
            if (null != originalFilename && originalFilename.length() > 0) {
                storageFilePath = tempPath + originalFilename;
                try {
                    Files.copy(multipartFile.getInputStream(), Paths.get(storageFilePath));
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new RuntimeException("文件上传失败,请重试!!");
                }

            }
        }
        return new ModelAndView("success");
    }
4.后端下载代码:

 /**
     * 文件下载
     * @param response 
     * @throws Exception
     */
    @RequestMapping(value = "/downFile")
    public void downFile(HttpServletResponse response)throws Exception{
        String pdfPath = "d:"+File.separator+ "temp" + File.separator + "aa.pdf";
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=aa.pdf");
        ServletOutputStream output = response.getOutputStream();

        BufferedOutputStream bufferOut = new BufferedOutputStream(output);
        InputStream inputStream = new FileInputStream(pdfPath);

        byte[] buffer = new byte[5 * 1024];
        int length = 0;
        while ((length = inputStream.read(buffer, 0, buffer.length)) != -1) {
            bufferOut.write(buffer, 0, length);
        }
        bufferOut.flush();
        bufferOut.close();
        output.close();
        inputStream.close();
    }







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值