文件上傳時的三個屬性很重要。加入現在上傳控件為:
private File upload;//封裝上傳文件域的屬性。注意這裡的upload是與
private String uploadContentType;//封裝上傳文件類型的屬性。形如 xxxContentType
private String uploadFileName;//封裝上傳文件名的屬性。xxxFileName.
開發步驟如下:
1、新建一個web工程,導入struts2上傳文件所需jar,如下圖

目錄結構

2、新建Action
第一種方式
package com.ljq.action;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class UploadAction extends ActionSupport{
private File image; //上傳的文件
private String imageFileName; //文件名稱
private String imageContentType; //文件類型
public String execute() throws Exception {
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
//D:\apache-tomcat-6.0.18\webapps\struts2_upload\images
System.out.println("realpath: "+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 File getImage() {
return image;
}
public void setImage(File image) {
this.image = image;
}
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public String getImageContentType() {
return imageContentType;
}
public void setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
}
}
第二種方式
package com.ljq.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class UploadAction2 extends ActionSupport {
// 封裝上傳文件域的屬性
private File image;
// 封裝上傳文件類型的屬性
private String imageContentType;
// 封裝上傳文件名的屬性
private String imageFileName;
// 接受依賴注入的屬性
private String savePath;
@Override
public String execute() {
FileOutputStream fos = null;
FileInputStream fis = null;
try {
// 建立文件輸出流
System.out.println(getSavePath());
fos = new FileOutputStream(getSavePath() + "\\" + getImageFileName());
// 建立文件上傳流
fis = new FileInputStream(getImage());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
} catch (Exception e) {
System.out.println("文件上傳失敗");
e.printStackTrace();
} finally {
close(fos, fis);
}
return SUCCESS;
}
/**
* 返回上傳文件的保存位置
*
* @return
*/
public String getSavePath() throws Exception{
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public File getImage() {
return image;
}
public void setImage(File image) {
this.image = image;
}
public String getImageContentType() {
return imageContentType;
}
public void setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
}
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
private void close(FileOutputStream fos, FileInputStream fis) {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
System.out.println("FileInputStream關閉失敗");
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
System.out.println("FileOutputStream關閉失敗");
e.printStackTrace();
}
}
}
}
struts.xml配置文件
/WEB-INF/page/message.jsp /images /WEB-INF/page/message.jsp /upload/upload.jsp image/bmp,image/png,image/gif,image/jpeg 1025956
上傳表單頁面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
文件上傳
顯示結果頁面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
上傳成功
上傳成功!
">