日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

struts1.2文件上传

發布時間:2025/7/14 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 struts1.2文件上传 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

jsp頁面

?

<!--上傳圖片時,在當前頁面顯示圖片 注意form中必須加 enctype="multipart/form-data"-->
<form action="XXX.do"?enctype="multipart/form-data">
<input type="file" name="file" onpropertychange="document.all.imgBook.src='file:///'+this.value" />
??? ?<div id="img">
??? ??<img id="imgBook" style="WIDTH: 110px; HEIGHT: 154px" height="154" src="" width="110"/>
??? ?</div>?
</form>

ActionForm


public class DownloadForm extends ActionForm {

?private String fileName;
?
?private FormFile file;

public FormFile getFile() {
??return file;
?}


?public void setFile(FormFile file) {
??this.file = file;
?}


?public String getFileName() {
??return fileName;
?}


?public void setFileName(String fileName) {
??this.fileName = fileName;
?}

}

?

Action
public class Download_AddAction extends Action {

?public ActionForward execute(ActionMapping mapping, ActionForm form,
???HttpServletRequest request, HttpServletResponse response) {
??DownloadForm downloadForm = (DownloadForm) form;// TODO Auto-generated method stub
??
//??上傳
????FormFile file = downloadForm.getFile();

????? //根據時間設置文件名
????Calendar calendar=Calendar.getInstance();
??? ????String time=calendar.get(Calendar.YEAR)+""+calendar.get(Calendar.DAY_OF_YEAR)+""+calendar.getTimeInMillis();
??? ????String fileName = time+file.getFileName();

????InputStream streamIn = null; //輸入輸出流
????OutputStream streamOut = null;
????String sysroot = servlet.getServletContext().getRealPath("/download/upload"); //獲取圖片文件夾路徑????
????String filePath = sysroot + "//" + fName; //最終路徑
????try {
?????streamIn = file.getInputStream(); //以下是上傳的代碼,不用變,固定的
?????streamOut = new FileOutputStream(filePath);
?????int bytesRead = 0;
?????byte[] buffer = new byte[20480];
?????while ( (bytesRead = streamIn.read(buffer, 0, 20480)) != -1) {
?????streamOut.write(buffer, 0, bytesRead);
?????}
?????streamOut.close();
?????streamIn.close();
????request.setAttribute("info", "上傳成功啦");
???
?????}
?????catch (FileNotFoundException e) {
?????request.setAttribute("error", "上傳失敗啦");
?????e.printStackTrace();
?????return mapping.findForward("jumpErrorPage");
?????}
?????catch (IOException e) {

?????request.setAttribute("error", "上傳失敗啦");
?????e.printStackTrace();
?????return mapping.findForward("jumpErrorPage");
?????}

?

?return mapping.findForward("jumpSuccessPage");
}

總結

以上是生活随笔為你收集整理的struts1.2文件上传的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。