第五課 文件上傳


上傳文件頁面,

 action處理頁面

package com.jing.action;

import java.io.File;
import java.io.IOException;
import java.util.*;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.jing.dao.*;
import com.jing.po.*;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class AddCommodityAction extends ActionSupport{
private List<CommodityClass> commodityClass;
private CommodityClassDao commodityClassDao;
private CommodityDao commodityDao;

private String commodityName; //商品名稱
private String commodityClassName; //商品種類
private String manufacturer; //生產廠家
private String commodityDesc; //商品描述
private Double commodityPrice; //商品價格
private Double EPrice; //網上價格
private File uploadImage; //臨時文件
private String uploadImageContentType; //上傳圖片臨時文件 類型
private String uploadImageFileName; //上傳圖片臨時文件 真實文件名





public String execute() {
//上傳文件開始
String realPath=ServletActionContext.getServletContext().getRealPath("admin/uploadImages");
System.out.println("RealPath---:"+realPath);
String dir=null;
if (uploadImage != null) {
File savefile = new File(new File(realPath), uploadImageFileName);
dir = savefile.getPath();
System.out.println("dir---:"+dir);
if (!savefile.getParentFile().exists())
savefile.getParentFile().mkdirs();
try {
FileUtils.copyFile(uploadImage, savefile);
} catch (IOException e) {
e.printStackTrace();
}
}
//上傳文件結束
//保存商品數據到數據庫 start
Commodity commodity = new Commodity();
commodity.setCommodityName(commodityName);
commodity.setCommodityClassName(commodityClassName);
commodity.setManufacturer(manufacturer);
commodity.setCommodityDesc(commodityDesc);
commodity.setCommodityPrice(commodityPrice);
commodity.setEPrice(EPrice);
commodity.setImage(dir);
commodityDao.save(commodity);
//保存商品數據 end
System.out.println("commodityName---:"+commodityName);
System.out.println("commodityClassName---:"+commodityClassName);
System.out.println("uploadImage---:"+uploadImage);
System.out.println("dir---:"+dir);
System.out.println("uploadImageContentType---:"+uploadImageContentType);
System.out.println("uploadImageFileName---:"+uploadImageFileName);
return SUCCESS;
}


public String getCommodityClassName() {
return commodityClassName;
}
public void setCommodityClassName(String commodityClassName) {
this.commodityClassName = commodityClassName;
}
public String getCommodityName() {
return commodityName;
}
public void setCommodityName(String commodityName) {
this.commodityName = commodityName;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getCommodityDesc() {
return commodityDesc;
}
public void setCommodityDesc(String commodityDesc) {
this.commodityDesc = commodityDesc;
}
public Double getCommodityPrice() {
return commodityPrice;
}
public void setCommodityPrice(Double commodityPrice) {
this.commodityPrice = commodityPrice;
}
public Double getEPrice() {
return EPrice;
}
public void setEPrice(Double ePrice) {
EPrice = ePrice;
}
public File getUploadImage() {
return uploadImage;
}
public void setUploadImage(File uploadImage) {
this.uploadImage = uploadImage;
}
public String getUploadImageContentType() {
return uploadImageContentType;
}
public void setUploadImageContentType(String uploadImageContentType) {
this.uploadImageContentType = uploadImageContentType;
}
public String getUploadImageFileName() {
return uploadImageFileName;
}
public void setUploadImageFileName(String uploadImageFileName) {
this.uploadImageFileName = uploadImageFileName;
}

public void setCommodityClassDao(CommodityClassDao commodityClassDao) {
this.commodityClassDao = commodityClassDao;
}
public List<CommodityClass> getCommodityClass() {
return commodityClass;
}
public void setCommodityClass(List<CommodityClass> commodityClass) {
this.commodityClass = commodityClass;
}

public void setCommodityDao(CommodityDao commodityDao) {
this.commodityDao = commodityDao;
}


}

struts.xml文件
<!-----------------------文件名:struts.xml------------------>   <struts>       <!-- Action所在包定義 -->       <package name="C04.4" extends="struts-default">           <!-- Action名字,類以及導航頁面定義 -->           <!-- 通過Action類處理才導航的的Action定義 -->           <action name="AddCommodityAction" class="com.jing.action.AddCommodityAction">               <result name="input">/jsp/upload.jsp</result>               <result name="success">/jsp/result.jsp</result>           </action>   <!—顯示配置文件上傳攔截器 -->   <interceptor-ref name=”fileUpload”>   <!—指定特定類型的上傳文件 -->   <param name =”allowedTypes”>text/plain,application/xml</param>   </ interceptor-ref >   <interceptor-ref name=”defaultStack”></ interceptor-ref >       </package>   </struts>   

定義了一個名為“allowedTypes”的參數,其中在<param></param>之間的是文件類型,也可以用“,”間隔,表示允許上傳多個文件類型。這里允許上傳文件類型為txt、xml格式的文件。如果讀者不知道各個文件類型的定義,可在自己的JBoss安裝目錄中的server\default\deploy\jboss-web.deployer\conf\下的web.xml文件中找到(搜索<mime-mapping>即可)。注意:如果顯示配置Struts2自己的缺省攔截器一定要寫在“defaultStack”前,否則“fileUpload”攔截器不會執行攔截。因為Struts2中如果某個攔截器執行攔截時候發現自己已經執行過,第二個乃至之后同名的攔截器都不會執行。這里因為“defaultStack”攔截器棧中包含了“fileUpload”攔截器,而“fileUpload”攔截器已經執行攔截了,則不會再執行攔截。如果把“defaultStack”攔截器棧放在“fileUpload”攔截器前配置,則只執行“defaultStack”攔截器棧中的“fileUpload”攔截器,這里是沒有定義“allowedTypes”的,Struts2缺省默認的是支持所有文件類型。因此它會支持所有文件類型的文件上傳。因此再設定“allowedTypes”就沒有任何意義了。

   

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
  © 2014-2022 ITdaan.com 联系我们: