package com.pk.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.commons.CommonsMultipartResolver; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Iterator; /** * Created by Administrator on 2014/9/5. */ @Controller @RequestMapping("file") public class UploadController { @RequestMapping("/upload.do") public String upload(HttpServletRequest request, HttpServletResponse response) throws IOException { // 這裏我用到了jar包 CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver( request.getSession().getServletContext()); if (multipartResolver.isMultipart(request)) { MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; Iterator<String> iter = multiRequest.getFileNames(); while (iter.hasNext()) { MultipartFile file = multiRequest.getFile((String) iter.next()); if (file != null) { String fileName = file.getOriginalFilename(); String path1 = Thread.currentThread() .getContextClassLoader().getResource("").getPath() + "download" + File.separator; // 下面的加的日期是爲了防止上傳的名字同樣 String path = path1 + new SimpleDateFormat("yyyyMMddHHmmss") .format(new Date()) + fileName; File localFile = new File(path); file.transferTo(localFile); } } } return "uploadSuccess"; } @RequestMapping("/toUpload.do") public String toUpload() { return "upload"; } @RequestMapping("/download") public String download(String fileName, HttpServletRequest request, HttpServletResponse response) { response.setCharacterEncoding("utf-8"); response.setContentType("multipart/form-data"); response.setHeader("Content-Disposition", "attachment;fileName=" + fileName); try { String path = Thread.currentThread().getContextClassLoader() .getResource("").getPath() + "download";//這個download目錄爲啥創建在classes下的 InputStream inputStream = new FileInputStream(new File(path + File.separator + fileName)); OutputStream os = response.getOutputStream(); byte[] b = new byte[2048]; int length; while ((length = inputStream.read(b)) > 0) { os.write(b, 0, length); } // 這裏主要關閉。 os.close(); inputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // 返回值要注意,要否則就出現下面這句錯誤! //java+getOutputStream() has already been called for this response return null; } }
還有 上傳 配置文件注意
html
<!-- mvc註解驅動 -->java
<mvc:annotation-driven/>web
<!-- 掃描器已經有了上面這個mvc註解驅動的功能了,全部不須要了 -->spring
<context:component-scan base-package="com.pk.web.controller" />mvc
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">app
<property name="defaultEncoding" value="utf-8" />jsp
<property name="maxUploadSize" value="104857600"/>post
<property name="maxInMemorySize" value="4096"/>this
</bean>url
<!-- 前綴+ viewName +後綴 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- webroot到某一指定的文件夾的路徑 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 本身在你的 /WEB-INF/jsp下創建給jsp見面吧。或者該我上面的配置 -->
<!-- 視圖名稱的後綴 -->
<property name="suffix" value=".jsp"></property>
</bean>
這裏是html 點擊下載,這裏就寫死了(你能夠修改爲你本身放的文件)。能夠修改的。
<a href="file/download.do?fileName=map.txt">下載1 </a><br />
下面這裏是上傳代碼
<form action="file/upload.do" method="post" enctype="multipart/form-data">
選擇文件<input type="file" name="file">
<input type="submit" value="上傳">
</form>
web.xml 配置文件
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- struts習慣使用/*,在springmvc無論用 -->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
每一個方法都返回的是一個ActionForward對象,而response是ActionForward對象參數,因此就會使response衝突!
因此處理上傳的action返回null就能夠了
最終我把action裏的最後 「return SUCCESS 」 改爲了 「return null 」 成功了 !!!
jar 包在 oschina 這裏不知道怎麼上傳過去給下載的。。同窗們本身網上找吧!