struts3.0的上傳改進和struts2--上傳和下載
阿新 • • 發佈:2019-02-07
package action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
//封裝檔案標題請求引數的屬性
private String title;
//封裝上傳檔案域的屬性
private File upload;
//封裝上傳檔案型別的屬性
private String uploadContentType;
//封裝上傳檔名的屬性
private String uploadFileName;
//直接在struts.xml檔案中配置的屬性
private String savePath;
public String upload() throws Exception
{
//以伺服器的檔案儲存地址和原檔名建立上傳檔案輸出流
FileInputStream fis = new FileInputStream(getUpload());
FileOutputStream fos = new FileOutputStream(getSavePath()
+ "\\" + getUploadFileName());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
{
fos.write(buffer , 0 , len);
}
fos.close();
fis.close();
return SUCCESS;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
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 getSavePath() {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
//接受struts.xml檔案配置值的方法
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
//封裝檔案標題請求引數的屬性
private String title;
//封裝上傳檔案域的屬性
private File upload;
//封裝上傳檔案型別的屬性
private String uploadContentType;
//封裝上傳檔名的屬性
private String uploadFileName;
//直接在struts.xml檔案中配置的屬性
private String savePath;
public String upload() throws Exception
{
//以伺服器的檔案儲存地址和原檔名建立上傳檔案輸出流
FileInputStream fis = new FileInputStream(getUpload());
FileOutputStream fos = new FileOutputStream(getSavePath()
+ "\\" + getUploadFileName());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
{
fos.write(buffer , 0 , len);
}
fos.close();
fis.close();
return SUCCESS;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
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 getSavePath() {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
//接受struts.xml檔案配置值的方法
}