springmvc文件上传和文件下载

来源:互联网 发布:云计算一般薪资多少 编辑:程序博客网 时间:2024/05/21 18:10

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
原创粉丝点击