spring 文件上传与下载【idea】

来源:互联网 发布:linux find mtime时间 编辑:程序博客网 时间:2024/06/07 13:01

controller

package com.bd.controller;import java.io.File;import java.io.IOException;import java.io.InputStream;import org.apache.commons.io.FileUtils;import org.springframework.http.HttpHeaders;import org.springframework.http.HttpStatus;import org.springframework.http.MediaType;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.multipart.MultipartFile;import com.bd.model.Emp;@Controller@RequestMapping("file")public class FileController {/** * 文件上传 * */@RequestMapping("upload")public String upload(@RequestParam("uploadFile") MultipartFile  uploadFile){try {InputStream    input = uploadFile.getInputStream();File  desFile = new File("d:/"+uploadFile.getOriginalFilename());FileUtils.copyInputStreamToFile(input, desFile);} catch (IOException e) {e.printStackTrace();}return "show";}/** * 文件下载 * */@RequestMapping("download")public ResponseEntity<byte[]> download(){String fileName = "测评编程题.doc";byte[] fileByte=null;try {String  dFileName = new String(fileName.getBytes("UTF-8"),"ISO-8859-1");HttpHeaders  header = new HttpHeaders();header.setContentType(MediaType.APPLICATION_OCTET_STREAM);header.setContentDispositionFormData("attachment", dFileName);fileByte = FileUtils.readFileToByteArray(new File("d:/"+fileName));return new ResponseEntity<byte[]>(fileByte,header,HttpStatus.CREATED);} catch (IOException e) {e.printStackTrace();}return null;}@RequestMapping("json")@ResponseBodypublic  Emp queryEmpById(){Emp  emp = new Emp();emp.setId(1);emp.setEmpName("张三");emp.setEmpNo("101");emp.setGender(1);emp.setSalary(6000);emp.setDeptNo("1001");return emp;}}

springMVC.xml 配置

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:context="http://www.springframework.org/schema/context"        xmlns:aop="http://www.springframework.org/schema/aop"        xmlns:tx="http://www.springframework.org/schema/tx"        xmlns:mvc="http://www.springframework.org/schema/mvc"       xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans.xsd            http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context.xsd          http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop.xsd         http://www.springframework.org/schema/tx     http://www.springframework.org/schema/tx/spring-tx.xsd     http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc.xsd">   <context:component-scan base-package="com.bd"></context:component-scan>   <tx:annotation-driven/>   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">      <property name="prefix" value="/WEB-INF/view/"></property>      <property name="suffix" value=".jsp"></property>   </bean>      <!-- 文件上传 -->      <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">      <property name="defaultEncoding" value="UTF-8"></property>      <property name="maxUploadSize" value="5000000"></property>   </bean>         <bean id="mappingJackson2JsonView" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"></bean>   </beans>