在eclipse中的javaEE环境下:导入必要的架包
web.xml的配置文件:
id="WebApp_ID" version="2.5">
springDispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
springDispatcherServlet
/
HiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
HiddenHttpMethodFilter
/*
spring的bean的配置文件springmvc.xml;
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
handler类方法:实现文件的上传和下载的方法
@Controllerpublic classSpringMVCTest {
@AutowiredprivateEmployeeDao employeeDao;//实现文件的下载//需要说明的是文件的上传和下载不需要其他配置
@RequestMapping("testResponseEntity")public ResponseEntity testResponseEntity(HttpSession session) throwsIOException{byte[] body=null;
ServletContext servletContext=session.getServletContext();///files/abc.txt:所要下载文件的地址
InputStream in=servletContext.getResourceAsStream("/files/abc.txt");
body=new byte[in.available()];
in.read(body);
HttpHeaders headers=newHttpHeaders();//响应头的名字和响应头的值
headers.add("Content-Disposition", "attachment;filename=abc.txt");
HttpStatus statusCode=HttpStatus.OK;
ResponseEntity response=new ResponseEntity(body, headers, statusCode);returnresponse;
}
//文件上传,
@RequestMapping("/testFileUpload")
public String testFileUpload(@RequestParam("desc") String desc,
@RequestParam("file") MultipartFile file) throws IOException{
System.out.println("desc:"+desc);
System.out.println("OriginalFilename"+file.getOriginalFilename());
System.out.println("InputStream"+file.getInputStream());
return "success";
}
}
jsp页面:index.jsp:
Insert title hereFile:
Desc:
success.jsp页面:显示文件上传成功
Insert title here