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

struts上傳圖片

發布時間: 2024-12-07 03:09:13

A. Struts2中上傳圖片後如何讓路徑顯示出來,

在action中定義一個變數,存儲上傳後的路徑,生成setter,getter方法 ,jsp 中就可以直接拿到了

B. 在java項目中上傳圖片時如何使上傳的圖片自動保存到指定路徑

用struts也可以實現 多文件上傳

下面是我寫的代碼,

參數清謹中有要保存的目錄

作為參考!

/*文答此基件目錄*/
public static String [] fileArray={
"扒簡logo.png",
"index.swf",
"OEMInfo.txt",
"favicon.ico"};

/**
* @author Caoshun
* @see 接收並保存文件
* */
public static void receiveAndSaveAllFileByPath(ActionForm form,String rootPath1,String rootPath2){
String fileName="";
//獲取表單中的文件資源
Hashtable<Object, Object> files = form.getMultipartRequestHandler().getFileElements();
//遍歷文件,並且循環保存
//當前處理文件序號
int file_num=1;
for (Enumeration<Object> e = files.keys(); e.hasMoreElements();) {

/*根據處理的當前文件下標,確定文件名*/
fileName=fileArray[file_num-1];

FormFile file = (FormFile) files.get((String) e.nextElement());
if (file != null && file.getFileSize() > 0) {
try {
//使用formfile.getInputStream()來獲取一個文件的輸入流進行保存。
//文件名
//String fileName = file.getFileName();
//System.out.println("debug in AddEnterpriceAction.java on line 152 fileName is : "+fileName);
//文件大小
//int fileSize = file.getFileSize();
//文件流
InputStream is = file.getInputStream();
//將輸入流保存到文件
//String rootPath = this.servlet.getServletContext().getRealPath("files");

//往cn中寫入
File rf = new File(rootPath1);
FileOutputStream fos = null;
fos = new FileOutputStream(new File(rf, fileName));
byte[] b = new byte[10240];
int real = 0;
real = is.read(b);
while (real > 0) {
fos.write(b, 0, real);
real = is.read(b);
}

//往en中寫入
File rf2 = new File(rootPath2);
InputStream is2 = file.getInputStream();
FileOutputStream fos2 = null;
fos2 = new FileOutputStream(new File(rf2, fileName));
byte[] b2 = new byte[10240];
int real2 = 0;
real2 = is2.read(b2);
while (real2 > 0) {
fos2.write(b2, 0, real2);
real2 = is2.read(b2);
}

//關閉文件流
fos.close();
is.close();
fos2.close();
is2.close();
} catch (RuntimeException e1) {
e1.printStackTrace();
} catch (Exception ee) {
ee.printStackTrace();
}
file.destroy();

}
file_num++;
}
}

熱點內容
大話2腳本製作 發布:2025-09-19 03:25:47 瀏覽:495
腳本精靈用的什麼語言 發布:2025-09-19 03:21:32 瀏覽:846
微型機常用的存儲器 發布:2025-09-19 03:18:17 瀏覽:468
迷你世界腳本編輯代碼在哪裡 發布:2025-09-19 03:17:40 瀏覽:373
我的世界110伺服器的天域組織 發布:2025-09-19 02:49:36 瀏覽:796
為什麼安卓手機使用久了會變卡 發布:2025-09-19 02:49:36 瀏覽:875
國家校時伺服器ip 發布:2025-09-19 02:45:18 瀏覽:921
安卓補幀軟體在哪裡下 發布:2025-09-19 02:45:17 瀏覽:32
安卓移機蘋果怎麼操作 發布:2025-09-19 01:58:55 瀏覽:163
我的世界國際版伺服器地址名稱 發布:2025-09-19 01:52:24 瀏覽:336