涪陵男性专科医院:struts2的文件上传与下载
来源:百度文库 编辑:偶看新闻 时间:2024/04/30 04:23:44
需要借助第三方包:FileUpload和 IO包。下地地址:http://commons.apache.org/
将这两个jar包放在项目的lib目录下,即可。struts2将这两个插件很好地整合在一起,可以很方便地实现文件上传与下载的功能。
对于上传文件而言,form表单中的method属性值一定要为post,并且将enctype=”multipart/form-data”属性必须写上。
以下是整个上传文件(单个文件)的流程及程序文件:
upload.jsp:
<%@ page language="java"contentType="text/html;charset=gbk"
pageEncoding="gbk"%>
<%@ taglibprefix="s"uri="/struts-tags"%>
username | |
password | |
file | |
UploadAction.java:
packagecom.test.upload;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.InputStream;
importjava.io.OutputStream;
importorg.apache.struts2.ServletActionContext;
importcom.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private String username;
private String password;
private File file;
//定义文件的文件名
private String fileFileName;
//定义文件的类型
private String fileContentType;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsernaem() {
return username;
}
public void setUsernaem(String username) {
this.username = username;
}
@Override
public String execute() throws Exception {
//创建文件输入流
InputStreamis = new FileInputStream(file);
//设置文件上传的目的位置
Stringroot = ServletActionContext.getRequest().getRealPath("/upload");
//定义一个目标文件
Filedestfile = newFile(root,this.getFileFileName());
//输出到目标文件
OutputStreamos = new FileOutputStream(destfile);
byte[] buffer = new byte[400];
int length = 0;
while((length = is.read(buffer))>0){
os.write(buffer,0,length);
}
is.close();
os.close();
return SUCCESS;
}
}
struts.xml:
"-//Apache Software Foundation//DTD StrutsConfiguration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
设置上传文件的大小。默认2097152是2M,可以自已进行设定-->
uploadResult.jsp:
<%@ page language="java"contentType="text/html;charset=gbk"
pageEncoding="gbk"%>
<%@ taglib prefix="s"uri="/struts-tags" %>
username:
password:
file:
文件上传当然也有一定的限制,比如:文件的大小(上传文件大小都默认为2M,当然,可以自己修改它的大小的限制。)、文件的属性等等。可以参看struts2-core-2.x.x.jar\struts-default.xml文件中的
分两种情况:1、指定用户能上传几个文件。2、不指定,由用户在客户端手动去改变。此方法更灵活。
1、下面就指定用户能上传几个文件方式(最主要的区别是将File类型变成List类型),来看如下的流程及程序代码:
upload.jsp
<%@ page language="java"contentType="text/html;charset=gbk"
pageEncoding="gbk"%>
<%@ taglibprefix="s"uri="/struts-tags"%>
username | |
password | |
file1 | |
file2 | |
file3 | |
UploadAction.java
packagecom.test.upload;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.util.List;
importorg.apache.struts2.ServletActionContext;
importcom.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private String username;
private String password;
//表示客户端要上传任意多个文件,并保存在list集合中
private List
//表示客户端要上传任意多个文件的文件名,并保存在list集合中
private List
//表示客户端要上传任意多个文件的文件类型,并保存在list集合中
private List
//它们三个集合中的顺序是一一对应
public List
return file;
}
public void setFile(List
this.file = file;
}
public List
return fileContentType;
}
public void setFileContentType(List
this.fileContentType = fileContentType;
}
public List
return fileFileName;
}
public void setFileFileName(List
this.fileFileName = fileFileName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsernaem() {
return username;
}
public void setUsernaem(String username) {
this.username = username;
}
@Override
public String execute() throws Exception {
for(int i=0;i //创建文件输入流.从集合中进行取得 InputStreamis = new FileInputStream(file.get(i)); //设置文件上传的目的位置 String root = ServletActionContext.getRequest(). //定义一个目标文件,从集合中进行取得 Filedestfile = newFile(root,this.getFileFileName().get(i)); //输出到目标文件 OutputStreamos = new FileOutputStream(destfile); byte[] buffer = new byte[400]; int length = 0; while((length = is.read(buffer))>0){ os.write(buffer,0,length); } is.close(); os.close(); } return SUCCESS; } } struts.xml (与上述的没有变化) ……. uploadResult.jsp(与上述的没有变化) …… 2、用户自己在客户端指定上传文件的个数(需要用到js): struts2对文件上传的拦截器,可以查看struts2-core-2.x.x.jar\org.apache.struts2.interceptor\FileUploadInterceptor.class文件和struts2-core-2.x.x.jar\struts-default.xml文件 upload.jsp <%@ page language="java"contentType="text/html;charset=gbk" pageEncoding="gbk"%> <%@ taglibprefix="s"uri="/struts-tags"%>getRealPath("/upload");
function addMore(){
var td = document.getElementById("more");
var br = document.createElement("br");
var input = document.createElement("input");
var button = document.createElement("input");
input.type="file";
input.name="file";
button.onclick=function(){
td.removeChild(br);
td.removeChild(input);
td.removeChild(button);
}
button.type="button";
button.value="Remove";
td.appendChild(br);
td.appendChild(input);
td.appendChild(button);
}
username | |
password | |
file | |
UploadAction.java(没有改变)
struts.xml
"-//Apache Software Foundation//DTD StrutsConfiguration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
maximumSize]可以从struts2-core-2.x.x.jar\org.apache.struts2.interceptor\FileUploadInterceptor.class中找到相关的定义 -->
application/vnd.ms-powerpoint
创建message.properties文件,来定义自己想要展现给用户的信息。
struts.messages.error.content.type.not.allowed=\u4e0a\u4f20\u6587\u4ef6\u7c7b\u578b\u4e0d\u6b63\u786e\uff0c\u8bf7\u91cd\u65b0\u9009\u62e9\uff01
struts.messages.error.file.too.large=\u4e0a\u4f20\u7684\u6587\u4ef6\u8fc7\u5927\uff01\u8bf7\u91cd\u8bd5\u3002
上面的两个key属性,在struts2-core-2.x.x.jar\struts-messages.properties文件中找到。value值,是自己定义的中文后,用native2ascii进行转换后的内容 -->
uploadResult.jsp(没有改变,与上面的内容一样)
文件的下载:(处理下载 的类:struts2-core-2.x.x.jar\org.apache.struts2.dispatcher\StreamResult.class)
download.jsp
<%@ page language="java"contentType="text/html;charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s"uri="/struts-tags" %>
DownloadAction.java
packagecom.test.download;
importjava.io.InputStream;
importorg.apache.struts2.ServletActionContext;
importcom.opensymphony.xwork2.ActionSupport;
public classDownloadAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID= 1L;
public InputStream getDownloadFile(){
returnServletActionContext.getServletContext().getResourceAsStream("/upload/struts2test.ppt");
}
public String execute() throws Exception{
return SUCCESS;
}
}
struts.xml:
"-//Apache Software Foundation//DTD StrutsConfiguration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
application/vnd.ms-powerpoint
filename="struts2test.ppt"
downloadFile