1.文件上传
//FileUpLoadAction .java
public class FileUpLoadAction {
private File image; //取得文件
private String imageFileName; //取得文件名
private String imageContentType; //取得文件类型
private File[] images; //取得多个文件
private String[] imagesFileName; //取得多个文件名
private String[] imagesContentType; //取得多个文件类型
public String executeSimple() throws Exception{ //上传单个文件
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
System.out.println(realpath);
if(image!=null){
File savefile = new File(new File(realpath), imageFileName);
if(!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs();
FileUtils.copyFile(image, savefile);
ActionContext.getContext().put("message", "上传成功");
}
return "success";
}
public String executeMutiple() throws Exception{ //上传多个文件
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
System.out.println(realpath);
if(image!=null){
File savedir = new File(realpath);
if(!savedir.exists()){
savedir.mkdirs();
}
for(int i = 0 ; i<image.length ; i++){
File savefile = new File(savedir, imageFileName[i]);
FileUtils.copyFile(image[i], savefile);
}
ActionContext.getContext().put("message", "上传成功");
}
return "success";
}
.....//getters and setters
}
//上传单个文件表单
<form action="upload_executeSimple" enctype="multipart/form-data" method="post">
文件:<input type="file" name="image"><br/>
<input type="submit" value="上传"/>
</form>
//上传多个文件表单, 注意enctype="multipart/form-data"
<form action="upload_executeMutiple" enctype="multipart/form-data" method="post">
文件1:<input type="file" name="images"><br/>
文件2:<input type="file" name="images"><br/>
文件3:<input type="file" name="images"><br/>
<input type="submit" value="上传"/>
</form>
//struts.xml
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<constant name="struts.multipart.maxSize" value="10701096"/>
<package name="upload" namespace="/" extends="struts-default">
<action name="upload_*" class="FileUpLoadAction" method="{1}">
<result name="success">/WEB-INF/page/message.jsp</result>
</action>
</package>
</struts>
2. 文件下载
public class DownloadAction extends ActionSupport {
private String fileName;
public void setFileName() {
String tempFileName = ServletActionContext.getRequest().getParameter("name");
try {
// 对tempFileName进行UTF-8解码
// 这里使用request.setCharacterEncoding解码无效.
// 只有解码了getDownloadFile()方法才能在下载目录下正确找到请求的文件
tempFileName = new String(tempFileName .getBytes("ISO-8859-1"), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
this.fileName = tempFileName ;
}
//此方法对应的是struts.xml文件中的:
// <param name="contentDisposition">attachment;filename="${fileName}"</param>
// 这个属性设置的是下载工具下载文件时显示的文件名,
// 要想正确的显示中文文件名,我们需要对fileName再次编码
//否则中文名文件将出现乱码,或无法下载的情况
public String getFileName() throws UnsupportedEncodingException {
fileName=new String(fileName.getBytes(),"ISO-8859-1");
return fileName;
}
//此方法对应的是struts.xml文件中的:
// <param name="inputName">downloadFile</param>
//返回下载文件的流,可以参看struts2的源码
public InputStream getDownloadFile() {
this.setFileName();
return ServletActionContext.getServletContext().getResourceAsStream("/upload/" + fileName);
}
public String execute() throws Exception {
return SUCCESS;
}
}
struts.xml 其中的EL表达式,是要注意的
<action name="download" class="com.test.action.DownloadAction">
<result name="success" type="stream">
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="inputName">downloadFile</param>
<!--对应DownloadAction.getDownloadFile()-->
</result>
</action>