使用Struts2的上传组件上传文件和附件到文件目录中。
public class FileUploadT extends ActionSupport implements ServletRequestAware,
ServletResponseAware {
/**
*
*/
private static final long serialVersionUID = 1L;
private File upload;
private String uploadContentType;
private String uploadFileName;
private String upload3ContentType;
private String upload3FileName;
private File uploadFile1;
private String fileName;
private String fid;
private String fileType;
private HttpServletRequest request;
private HttpServletResponse response;
public FileUploadT() {
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public File getUploadFile1() {
return uploadFile1;
}
public void setUploadFile1(File uploadFile1) {
this.uploadFile1 = uploadFile1;
}
public String getFid() {
return fid;
}
public void setFid(String fid) {
this.fid = fid;
}
public String getFileType() {
return fileType;
}
public void setFileType(String fileType) {
this.fileType = fileType;
}
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 getUpload3ContentType() {
return upload3ContentType;
}
public void setUpload3ContentType(String upload3ContentType) {
this.upload3ContentType = upload3ContentType;
}
public String getUpload3FileName() {
return upload3FileName;
}
public void setUpload3FileName(String upload3FileName) {
this.upload3FileName = upload3FileName;
}
public void setServletRequest(HttpServletRequest arg0) {
// TODO Auto-generated method stub
this.request = arg0;
}
public void setServletResponse(HttpServletResponse arg0) {
// TODO Auto-generated method stub
this.response = arg0;
}
public void upLoadToUrl() {
String fileDirectory = ServletActionContext.getServletContext().getRealPath("/")+"fileDirectory\\";//文件保存到服务器磁盘上的路径
String attachDirectory = ServletActionContext.getServletContext().getRealPath("/")+"attachFileDirectory\\";//附件保存到服务器磁盘上的路径
FileInputStream fis = null;
FileOutputStream fos = null;
File attach ;
File files;
int bt = 0;
try {
MultiPartRequestWrapper wrapper = (MultiPartRequestWrapper)request;//使用Struts的MultiPartRequestWrapper来解析请求
Enumeration<String> e = wrapper.getFileParameterNames();
while(e.hasMoreElements()){
String name = e.nextElement();
if(name.equals("uploadFile1")){//如果是文档,就将文档保存到存放文档的目录中
files = wrapper.getFiles(name)[0];
fis = new FileInputStream(files);
fos = new FileOutputStream(new File(fileDirectory+fileName+"."+fileType));
byte[] buffer = new byte[1024 * 1024];
while ((bt = fis.read(buffer)) != -1) {
fos.write(buffer,0,bt);
}
fis.close();
fos.close();
}
if(name.equals("upload"))
{
attach = wrapper.getFiles(name)[0];
String attachname = wrapper.getFileNames(name)[0];
fis = new FileInputStream(attach);
fos = new FileOutputStream(new File(attachDirectory+attachname));//如果是附件,将附件保存到存放附件目录中
byte[] buffer = new byte[1024 * 1024];
while ((bt = fis.read(buffer)) != -1) {
fos.write(buffer,0,bt);
}
fis.close();
fos.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
|