先看下文件結構
下面是 源碼及其相關注釋
package com;
import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DownLoadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String savePath = "file//";//文件上傳的路徑
private String fileFileName;
private File file;
private String fileContentType;
public String getFileName() {
return fileFileName;
}
public void setFileName(String fileName) {
this.fileFileName = fileName;
}
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 getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
@Override
public String execute() throws Exception {
fileFileName = ServletActionContext.getRequest().getParameter(
"fileName");
return SUCCESS;
}
// 從下載文件原始存放路徑讀取得到文件輸出流
public InputStream getDownloadFile() throws UnsupportedEncodingException {
fileFileName = new String(fileFileName.getBytes("ISO8859-1"), "UTF-8");
System.out.println("fileFileName:Down:"+fileFileName);
return ServletActionContext.getServletContext().getResourceAsStream(
savePath + fileFileName);
}
// 如果下載文件名為中文,進行字符編碼轉換
public String getDownloadChineseFileName() {
String downloadChineseFileName = fileFileName;
try {
downloadChineseFileName = new String(downloadChineseFileName
.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downloadChineseFileName;
}
}
package com;
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 com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private File[] uploadFile;//上傳的文件名
private String[] contentType;
private String[] caption;
private String[] uploadFilename;
public String[] getCaption() {
return caption;
}
public void setCaption(String[] caption) {
this.caption = caption;
}
public String[] getContentType() {
return contentType;
}
public void setUploadFileContentType(String[] contentType) {
this.contentType = contentType;
}
public File[] getUploadFile() {
return uploadFile;
}
public void setUploadFile(File[] uploadFile) {
this.uploadFile = uploadFile;
}
public String[] getUploadFilename() {
return uploadFilename;
}
public void setUploadFileFileName(String[] uploadFilename) {
this.uploadFilename = uploadFilename;
}
/**
* 拷貝文件到相應的目錄下
* @param src
* @param dst
*/
private static void copy(File src, File dst) {
try {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src));
out = new BufferedOutputStream(new FileOutputStream(dst));
byte[] buffer = new byte[16 * 1024];
while (in.read(buffer) > 0) {
out.write(buffer);
}
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public String execute() {
for (int i = 0; i < this.getUploadFile().length; i++) {
File imageFile = null;
imageFile = new File(ServletActionContext.getServletContext()