上传多附件:
jsp:
<input type="file" name="upload" />
<input type="file" name="upload" />
action:
private File[] upload;
private String[] uploadContentType;
private String[] uploadFileName;
private String savePath =File.separator+"uploadFile"+File.separator+"appendix";
public void setSavePath(String value{
this.savePath = value;
}
private String getSavePath() throws Exception{
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setUpload(File[] upload) {
this.upload = upload;
}
public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}
public File[] getUpload() {
return (this.upload);
}
public String[] getUploadContentType() {
return (this.uploadContentType);
}
public String[] getUploadFileName() {
return (this.uploadFileName);
}
//获取文件
File[] files = getUpload();
FileOutputStream fos = null;
FileInputStream fis = null;
String dataPath = getSavePath()+File.separator;
if(files!=null){
for (int i = 0 ; i < files.length ; i++){
//把文件保存到磁盘
fos = new FileOutputStream( dataPath+getUploadFileName()[i]);
fis = new FileInputStream(files[i]);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0){
fos.write(buffer , 0 , len);
}
}
fos.close();
fis.close();
}
下载:
jsp:
<a href="down_load.action?fileName=${uploadFileName}">${uploadFileName}</a>
aciton:
package com.ptnetwork.demandManagerSystem.web.action.admin;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage("admin")
@Results({ @Result(name = "success", type = "stream", params = {
"contentType", "application/octet-stream;charset=ISO8859-1",
"inputName", "inputStream",
"contentDisposition", "attachment;filename="${downloadFileName}"",
"bufferSize", "4096" }) })
public class DownLoadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String fileName;
private String inputPath="/uploadFile/appendix/";// 指定要被下载的文件路径
public void setFileName(String fileName) throws UnsupportedEncodingException {
// System.out.println("---------fileName-----"+fileName);
this.fileName = fileName;
}
public void setInputPath(String inputPath) {
this.inputPath = inputPath;
}
// 如果下载文件名为中文,进行字符编码转换
public String getDownloadFileName() {
String downloadFileName = fileName;
try {
downloadFileName = new String(downloadFileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downloadFileName;
}
public InputStream getInputStream() throws Exception {
return ServletActionContext.getServletContext().getResourceAsStream(
inputPath+fileName);
}
public String execute() throws Exception {
return SUCCESS;
}
}