當前位置:首頁 » 文件管理 » struts圖片上傳實例

struts圖片上傳實例

發布時間: 2023-05-25 20:08:44

㈠ 怎麼用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>
.....

這樣就可以啦

熱點內容
手機陰陽師腳本輔助 發布:2025-07-11 14:43:13 瀏覽:14
php通知 發布:2025-07-11 14:28:53 瀏覽:166
溯源碼燕窩多少錢一克 發布:2025-07-11 14:18:06 瀏覽:221
佳能相機格式化存儲卡 發布:2025-07-11 14:18:02 瀏覽:314
蛾群演算法 發布:2025-07-11 14:10:36 瀏覽:109
互聯網java 發布:2025-07-11 14:08:47 瀏覽:388
伺服器程序設計是什麼 發布:2025-07-11 14:07:58 瀏覽:283
html5壓縮圖片上傳 發布:2025-07-11 13:50:02 瀏覽:691
奧迪上傳FD 發布:2025-07-11 13:49:58 瀏覽:705
ktv套餐配置比較重要的是哪些 發布:2025-07-11 13:48:37 瀏覽:116