當前位置:首頁 » 文件管理 » 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++;
}
}

熱點內容
圖片上傳雲端 發布:2025-03-15 17:37:26 瀏覽:458
郵件伺服器ip池 發布:2025-03-15 17:31:51 瀏覽:397
php空間免費 發布:2025-03-15 17:09:39 瀏覽:965
安卓現在優化的怎麼樣 發布:2025-03-15 17:06:10 瀏覽:82
埃爾法2022款配置怎麼選 發布:2025-03-15 16:57:38 瀏覽:913
飛車什麼時候上線安卓 發布:2025-03-15 16:28:45 瀏覽:29
linuxpython命令行 發布:2025-03-15 16:19:45 瀏覽:960
編譯androidx86 發布:2025-03-15 16:19:01 瀏覽:787
戴爾g15怎麼切換溫度配置文件 發布:2025-03-15 16:16:18 瀏覽:852
長江存儲0 發布:2025-03-15 16:15:35 瀏覽:48