struts上传下载必须引入两个jar文件:
commons-fileupload-x.x.x.jar和comons-io-x.x.x.jar上传文件
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream; import org.apache.struts2.ServletActionContext; import pojo.User; import com.opensymphony.xwork2.ActionSupport; public class FileUpAction extends ActionSupport{
private static final int BUFFER_SIZE = 40*40;
private File upload;
private String uploadContentType;
private String uploadFileName;
private String savePath;
private User user;
private static void copy(File source, File target){
InputStream is = null;
OutputStream os = null;
try{
is = new BufferedInputStream(new FileInputStream(source), BUFFER_SIZE);
os = new BufferedOutputStream(new FileOutputStream(target), BUFFER_SIZE);
int len = 0;
byte[] bs = new byte[BUFFER_SIZE];
while ((len=is.read(bs))!=-1) {
os.write(bs, 0, len);
}
}catch (Exception e) {
e.printStackTrace();
}finally{
if(is!=null){
try{
is.close();
}catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
if(os!=null){
try{
os.close();
}catch (Exception e3) {
// TODO: handle exception
e3.printStackTrace();
}
}
}
} @Override
public String execute() throws Exception{
String path = ServletActionContext.getServletContext().getRealPath(this.getSavePath())+"\\"+this.getUploadFileName();
user.setPhone(this.uploadFileName);
File target = new File(path);
copy(this.upload, target);
return SUCCESS;
} public File getUpload() {
return upload;
} public void setUpload(File upload) {
this.upload = upload;
} public String getUploadContentType() {
return uploadContentType;
} public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
} public String getUploadFileName() {
return uploadFileName;
} public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
} public String getSavePath() {
return savePath;
} public void setSavePath(String savePath) {
this.savePath = savePath;
} public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
}
}
struts2中的action配置
<action name="fileUp" class="actions.FileUpAction">
<param name="savePath">/upload</param>
<result>/userInfo.jsp</result>
</action>
表单
<s:form action="fileUp" namespace="/" method="post" enctype="multipart/form-data">
<s:textfield name="user.name" label="姓名" size="20"/>
<s:file name="upload" label="形象" size="20"/>
<s:textfield name="user.age" label="年龄" size="20"/>
<s:radio list="#{1:'男',2:'女' }" name="user.sex" listKey="key" listValue="value" value="1" label="性别" cssStyle="border:0px;"/>
<s:textfield name="user.icard" label="身份证号" size="20"/>
<s:textfield name="user.phone" label="联系电话" size="20"/>
<s:textfield name="user.address" label="家庭住址" size="20"/>
<s:submit value="确定录入" align="center"/>
</s:form>
如果表单中包含一个name属性为xxx的文件域,那么在Action中可以使用如下3个属性来封装文件域信息:
File xxx 封装文件域对应的文件内容
String xxxContextType 封装文件域对应文件的文件类型
String xxxFileName 封装文件域对应文件的文件名
使用图片的时候
<img src="upload/<s:property value="uploadFileName"/>"/>
下载
import com.opensymphony.xwork2.ActionSupport; public class DownloadAction extends ActionSupport{
private String downPath;
public InputStream getInputStream() throws Exception{
return ServletActionContext.getServletContext().getResourceAsStream(downPath);
}
public String getDownPath() {
return downPath;
}
public void setDownPath(String downPath) {
this.downPath = downPath;
}
public String getDownloadFileName(){
String downFileName = downPath.substring(7);
try{
downFileName = new String(downFileName.getBytes(), "ISO8859-1");
}catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
return downFileName;
} @Override
public String execute() throws Exception{
return SUCCESS;
}
struts2.xml配置
<action name="downLoad" class="actions.DownloadAction">
<result type="stream">
<param name="contentType">
application/msword,text/plain,application/vnd.ms-powerpoint,application/vnd.ms-excel
</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">
attachment;filename="${downloadFileName}"
</param>
<param name="bufferSize">40960</param>
</result>
</action>
使用
<a href="downLoad.action?downPath=upload/123.png">下载</a>