struts圖片上傳實例
㈠ 怎麼用struts2實現多圖片上傳
新建Web Project,在WebRoot下新建upload文件夾
在WebRoot下新建upload.jsp,上傳界面
編寫上傳成功、失敗的提示界面。
在WebRoot下新建uploadError.jsp
在WebRoot下新建 uploadSuccess.jsp
編寫Action類
配置struts.xml文件,重置fileUpload攔截器。
測試,測試完成之後在tomcat下面webapps目錄下找到項目對應的文件夾下的upload下查看
㈡ struts上傳多張圖片怎麼做,要求可以說上傳中文的,而且圖片可以重復上傳不被覆蓋
你指的是一次上傳多張嗎?我這有個自己寫的例子:
//上傳實體類
public class Upload {
private File photoes; //封裝上傳文件的屬性
private String photoesFileName; //封裝上傳文件的名稱屬性
private String photoesContentType; //封裝上傳文件的類型屬性
private String targetdir; //保存路徑
private String targetfilename; //保存的文件名
public File getPhotoes() {
return photoes;
}
public void setPhotoes(File photoes) {
this.photoes = photoes;
}
public String getPhotoesFileName() {
return photoesFileName;
}
public void setPhotoesFileName(String photoesFileName) {
this.photoesFileName = photoesFileName;
}
public String getPhotoesContentType() {
return photoesContentType;
}
public void setPhotoesContentType(String photoesContentType) {
this.photoesContentType = photoesContentType;
}
public String getTargetdir() {
return targetdir;
}
public void setTargetdir(String targetdir) {
this.targetdir = targetdir;
}
public String getTargetfilename() {
return targetfilename;
}
public void setTargetfilename(String targetfilename) {
this.targetfilename = targetfilename;
}
}
這是上傳圖片的Action類中的部分代碼
//獲得伺服器上保存上傳文件的目錄images的絕對路徑
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
//設置保存文件的目錄
uld.setTargetdir(realpath);
//設置目標文件名
uld.setTargetfilename(generateFileName(uld.getPhotoesFileName()));
//把doc內容復制到target
FileUtils.File(uld.getPhotoes(), new File(uld.getTargetdir(),uld.getTargetfilename()));
㈢ struts2上傳圖片
可以直接在上傳的時候就創建一個tomcat下的目錄吧 圖片都放裡面
㈣ 在Struts2中如何上傳圖片並將剛剛上傳的圖片立即顯示出來
//action的關鍵代碼
import com.xt.gyz.facelook.service.FacesService;
public class ShowPic extends ActionSupport {
private InputStream is;
private Integer id;
private FacesService facesService;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public void setFacesService(FacesService facesService) {
this.facesService = facesService;
}
public InputStream getIs() {
return is;
}
public void setIs(InputStream is) {
this.is = is;
}
@Override
public String execute() throws Exception {
Faces face=facesService.findById(id);
is=new ByteArrayInputStream(face.getPic());
// FileOutputStream fos=new FileOutputStream(new File("E:/body.jpg"));
//頃返 System.out.println(fos);
// System.out.println("face:"+face.getPic().length);
//圓乎消 fos.write(face.getPic());
// fos.flush();
// fos.close();
return super.execute();
}
}
//不要忘記struts.xml配置
......
<action name="showPic" class="showPicAction">
<result type="stream">
<param name="inputName">is</param>
<param name="contectType">image/jpg</橘知param>
</result>
</action>
.....
這樣就可以啦