springMVC3.2ファイルのアップロードとダウンロードを簡単に実現
9398 ワード
今日はいろいろな手段でspringmvcを简単に実现してファイルのアップロードとダウンロードの机能を実现して、特にここに记录して后で検索することに备えて、そして各位の大神大牛にコードと设计の中の不足を指导してもらいます.
ファイルのアップロード------ライブラリ内のすべてのファイルを表示------ダウンロードボタンをクリックしてダウンロード
ソースコードは添付ファイルにあります
web.xmlコード:
Springmvcプロファイルコードspring-servlet.xml
ファイルのアップロードページjsp
すべてのファイルを表示するページ
ファイルのアップロード、表示、ダウンロードの制御クラス
ファイルのアップロード------ライブラリ内のすべてのファイルを表示------ダウンロードボタンをクリックしてダウンロード
ソースコードは添付ファイルにあります
web.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">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<!-- spring mvc start -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- spring mvc end -->
<!-- start-->
<filter>
<filter-name>Set Character Encoding</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>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- end-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Springmvcプロファイルコードspring-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="springapp" />
<mvc:annotation-driven />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/view/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- start -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1000000" />
</bean>
<!-- end -->
</beans>
ファイルのアップロードページjsp
<form action="fileupload.do" method="post" enctype="multipart/form-data">
file:<input type="file" name="file"/><br/>
<input type="submit" value="submit"/>
</form>
すべてのファイルを表示するページ
<c:forEach items="${files}" var="file">
<form action="downloadFile.do" method="post" >
<label>${file.name}</label>
<input type="submit" value=" "/>
<input type="hidden" name ="fileName" value="${file.name}">
</form>
</c:forEach>
ファイルのアップロード、表示、ダウンロードの制御クラス
package springapp.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
@Controller
public class FileAction implements ServletContextAware {
private ServletContext servletContext;
/**
*
*
* @param request
* @param mrequest
* @param response
* @return
* @throws IOException
*/
@RequestMapping(value = "fileupload.do", method = RequestMethod.POST)
public String fileUpload(HttpServletRequest request, Model model,
MultipartHttpServletRequest mrequest, HttpServletResponse response)
throws IOException {
MultipartFile file = mrequest.getFile("file");
String fileName = file.getOriginalFilename();
String path = this.servletContext.getRealPath("temp");
// System.out.println(path);
String pathAndfileName = path + "\\" + fileName;
// System.out.println(pathAndfileName);
if (!file.isEmpty()) {
this.copyFile(file.getInputStream(), pathAndfileName);
return "redirect:showFile.do";
}
model.addAttribute("msg", " ");
return "uploadFile";
}
/**
*
*
* @param in
* @param fileName
* @throws IOException
*/
public void copyFile(InputStream in, String filePathAndName)
throws IOException {
FileOutputStream fos = new FileOutputStream(filePathAndName);
byte[] buffer = new byte[1024 * 1024];
int bytesum = 0;
int byteread = 0;
while ((byteread = in.read(buffer)) != -1) {
bytesum += byteread;
fos.write(buffer, 0, byteread);
fos.flush();
}
fos.close();
in.close();
}
public void setServletContext(ServletContext arg0) {
// TODO Auto-generated method stub
this.servletContext = arg0;
}
/**
*
*
* @param model
* @param request
* @param response
* @return
*/
@RequestMapping("showFile.do")
public String showFile(Model model, HttpServletRequest request,
HttpServletResponse response) {
String fileAddress = this.servletContext.getRealPath("temp");
File[] files = new File(fileAddress).listFiles();
List<File> fileList = new ArrayList<File>();
for (File file : files) {
fileList.add(file);
}
model.addAttribute("files", fileList);
return "showFile";
}
/**
*
* @param response
* @param fileName
* @throws IOException
*/
@RequestMapping(value = "downloadFile.do", method = RequestMethod.POST)
public void downLoadFile(HttpServletResponse response,
@RequestParam("fileName")
String fileName) throws IOException {
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", "attachment;fileName="
+ new String(fileName.getBytes("gb2312"), "iso8859-1"));
File file = new File(fileName);
String path = this.servletContext.getRealPath("temp");
InputStream in = new FileInputStream(path + "\\" + file);
OutputStream os = response.getOutputStream();
byte[] b = new byte[1024 * 1024];
int length;
while ((length = in.read(b)) > 0) {
os.write(b, 0, length);
}
in.close();
}
}