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

熱點內容
toppython 發布:2025-07-13 16:34:05 瀏覽:904
安卓微信好友刪除怎麼找回來 發布:2025-07-13 16:28:10 瀏覽:123
華為微信自動存儲 發布:2025-07-13 16:26:45 瀏覽:195
svn外網不能訪問 發布:2025-07-13 16:26:33 瀏覽:724
易語言dll加密 發布:2025-07-13 16:17:50 瀏覽:808
java編寫記事本程序 發布:2025-07-13 16:12:13 瀏覽:663
辦公室如何做數據伺服器 發布:2025-07-13 15:55:24 瀏覽:327
用一句話證明我很窮ftp 發布:2025-07-13 15:54:48 瀏覽:936
安卓如何啟動畫面 發布:2025-07-13 15:43:22 瀏覽:643
安卓哪個娃娃 發布:2025-07-13 15:35:52 瀏覽:142