spring mvc 文件下的上传
@RequestMapping("/down")
public void down(HttpServletRequest request, HttpServletResponse response) throws Exception {
String message = null;
String fileName = request.getSession().getServletContext().getRealPath("/")+"/upload/"+request.getParameter("fileName");
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\"" + request.getParameter("fileName") + "\"");
// 循环取出流中的数据
byte[] b = new byte[100];
int len;
while ((len = fis.read(b)) > 0){
response.getOutputStream().write(b, 0, len);
}
fis.close();
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="net.spring.controller" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/hello.html"/>
<bean class="net.spring.interceptor.HelloWorldInterceptor1"/>
</mvc:interceptor>
</mvc:interceptors>
<!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="2000000000"></property>
</bean>
<!-- SpringMVC在超出上传文件限制时,会抛出org.springframework.web.multipart.MaxUploadSizeExceededException -->
</beans>
首先spring-servlet.xml的文件配置
xml文件的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Spring3MVC</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
单文件的同步上传:
//单文件上传 @RequestMapping("/uploadOne") public void uploadOne(HttpServletRequest request, HttpServletResponse response) throws IOException { String path = request.getSession().getServletContext().getRealPath("/upload"); MultipartHttpServletRequest mhs = (MultipartHttpServletRequest )request; CommonsMultipartFile cmf = ( CommonsMultipartFile)mhs.getFile("fileName"); String name = null; FileItem fi = cmf.getFileItem(); name = cmf.getOriginalFilename(); InputStream fis = fi.getInputStream(); mhs.getFile("fileName"); File file = new File(path); if(!file.exists()){ file.mkdirs(); } int i=0; FileOutputStream fos = new FileOutputStream(new File(path+"/"+name)); while((i=fis.read())!=-1){ fos.write(i); } fis.close(); fos.close(); }
多文件的同步上传:
//多文件上传 @RequestMapping("/upload") public void upload(HttpServletRequest request, HttpServletResponse response) throws IOException { String filePath = request.getContextPath(); String path = request.getSession().getServletContext().getRealPath("/upload"); String message = null; MultipartHttpServletRequest mhs = (MultipartHttpServletRequest )request; List<MultipartFile> list = mhs.getFiles("fileName"); String name = null; try { File file = new File(path); if(!file.exists()){ file.mkdirs(); } FileOutputStream fos = null; for(int i = 0 ; i < list.size(); i++){ name = list.get(i).getOriginalFilename(); fos = new FileOutputStream(new File(path+"/"+name)); fos.write(list.get(i).getBytes()); } fos.close(); message = "<a herf='javascript:void(0)' onclick='"+path+"/"+name+"'>" + "hello.jsp" + "</a>"; } catch (FileNotFoundException e1) { e1.printStackTrace(); } try { response.getWriter().print(message); } catch (Exception e) { e.printStackTrace(); } }
多文件的异步上传:需要用到ajaxupload.js插件
//多文件异步上传 @RequestMapping("/ajaxupload") public void ajaxupload(HttpServletRequest request ,HttpServletResponse response) throws Exception { String path = request.getSession().getServletContext().getRealPath("/upload"); String message = null; MultipartHttpServletRequest mhs = (MultipartHttpServletRequest )request; List<MultipartFile> list = mhs.getFiles("formFile"); SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS"); Date d = new Date(); String id = format.format(d).toString(); String name = null; File file = new File(path); if(!file.exists()){ file.mkdirs(); } FileOutputStream fos = null; for(int i = 0 ; i < list.size(); i++){ name = list.get(i).getOriginalFilename(); fos = new FileOutputStream(new File(path+"/"+name)); fos.write(list.get(i).getBytes()); } message = "{\"id\":\""+id+"\",\"fjmc\":\""+name+"\"}"; fos.close(); response.getWriter().write(message); }
页面的html代码,此处只是示例
文件的下载,文件的删除
//删除上传图片
@RequestMapping("/deletePhoto")
public void deletePhoto(HttpServletRequest request, HttpServletResponse response) throws IOException {
String fjmc = request.getParameter("fjmc");
String path = request.getSession().getServletContext().getRealPath("/")+"upload/"+fjmc;
File file = new File(path);
if(file.exists()){
file.delete();
}
}
浙公网安备 33010602011771号