JavaWeb中struts2实现文件上传下载功能实例解析
在JavaWeb应用中,实现文件的上传下载是很常见的需求。而在struts2框架中,实现文件的上传下载也并不难。本文将详细介绍struts2框架中如何实现文件上传下载的功能。
1. 文件上传实现
在struts2框架中,文件上传的实现需要用到Commons-Fileupload
和Commons-Io
两个Jar包。以下是文件上传的实现步骤:
- pom.xml中导入以下两个依赖:
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
- 在struts.xml中配置文件上传的interceptor和action:
<interceptors>
<interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>
<interceptor-stack name="myStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="fileUpload"/>
</interceptor-stack>
</interceptors>
<action name="fileUploadAction" class="com.example.action.FileUploadAction" method="upload">
<interceptor-ref name="myStack"></interceptor-ref>
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
- 在action类中实现upload()方法并处理文件上传:
public String upload() throws Exception {
List<File> fileUpload = new ArrayList<File>();
List<String> fileUploadContentType = new ArrayList<String>();
List<String> fileUploadFileName = new ArrayList<String>();
for(int i=0; i<upload.size(); i++) {
fileUpload.add(upload.get(i));
fileUploadContentType.add(uploadContentType.get(i));
fileUploadFileName.add(uploadFileName.get(i));
}
if(fileUpload != null && fileUpload.size() > 0) {
for(int i=0; i<fileUpload.size(); i++) {
File file = fileUpload.get(i); // 得到上传的文件
String fileName = fileUploadFileName.get(i); // 得到上传的文件名
InputStream is = new FileInputStream(file);
OutputStream os = new FileOutputStream(new File("C:\\fileuploads\\" + fileName));
byte[] buffer = new byte[1024];
int length = 0;
while((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
os.close();
is.close();
}
}
return SUCCESS;
}
2. 文件下载实现
实现文件的下载同样需要用到Commons-Io
Jar包。以下是文件下载的实现步骤:
- 在action类中实现download()方法:
public String download() throws Exception {
File file = new File("C:\\fileuploads\\" + fileName);
InputStream is = new FileInputStream(file);
ServletActionContext.getResponse().setContentType("application/octet-stream");
fileName = new String(fileName.getBytes(), "ISO8859-1"); // 解决中文文件名下载问题
ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment;filename=" + fileName);
ServletOutputStream os = ServletActionContext.getResponse().getOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while((len = is.read(buffer)) > 0) {
os.write(buffer, 0, len);
}
is.close();
os.close();
return null;
}
- 在struts.xml中配置download()方法的action:
<action name="download" class="com.example.action.FileUploadAction" method="download">
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename=${fileName}</param>
<param name="bufferSize">1024</param>
</result>
</action>
3. 示例
以下是一个简单的示例,首先是上传文件:
<form action="fileUploadAction.action" method="post" enctype="multipart/form-data">
<input id="upload" name="upload" type="file" multiple="multiple"/>
<input type="submit" value="上传"/>
</form>
上传成功后,可以在页面上看到上传的文件名,点击文件名可以下载上传的文件:
<a href="/download.action?fileName=${fileName}">${fileName}</a>
以上就是struts2中实现文件上传下载功能的完整攻略。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:JavaWeb中struts2实现文件上传下载功能实例解析 - Python技术站