java中的接口是类吗
248
2023-06-10
在SpringMVC框架下实现文件的上传和下载示例
在eclipse中的javaEE环境下:导入必要的架包
web.xml的配置文件:
xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
spring的bean的配置文件springmvc.xml;
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xmlns:mvc="http://springframework.org/schema/mvc" xsi:schemaLocation="http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.0.xsd http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd"> class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xmlns:mvc="http://springframework.org/schema/mvc"
xsi:schemaLocation="http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd">
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
handler类方法:实现文件的上传和下载的方法
@Controller
public class SpringMVCTest {
@Autowired
private EmployeeDao employeeDao;
//实现文件的下载
//需要说明的是文件的上传和下载不需要其他配置
@RequestMapping("testResponseEntity")
public ResponseEntity
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=new HttpHeaders();
//响应头的名字和响应头的值
headers.add("Content-Disposition", "attachment;filename=abc.txt");
HttpStatus statusCode=HttpStatus.OK;
ResponseEntity
return response;
}
//文件上传,
@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:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
File:
Desc:
success.jsp页面:显示文件上传成功
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~