我是最新的glassfish(3.1.2) - 所以不需要Apache的FileItem,也沒有getPart()的錯誤。我讀過上傳圖像的最佳做法是將它們保存在文件系統上(例如參見here)。我編輯現有的代碼 - 臭那個 - 所以我有想法做:Glassfish - 上傳圖片 - 正確地做
Part p1 = request.getPart("file");
System.out.println("!!!!!P1 : " + p1);
打印:
!!!!!P1 : File name=DSC03660.JPG,
StoreLocation=C:\_\glassfish3\glassfish\domains\domain1\generated\jsp\elkethe\upload_7cb06306_138b413999a__7ffa_00000000.tmp,
size=2589152bytes, isFormField=false, FieldName=file
換行符礦。在代碼的人絡繹不絕:
if (request.getParameter("crop") != null) {
// get path on the server
String outputpath = this.getServletContext().getRealPath(
"images/temp/" + session.getId() + ".jpg");
// store photo
InputStream is = p1.getInputStream();
createPhoto(is, outputpath);
session.setAttribute("photo_path", "images/temp/" + session.getId()
+ ".jpg");
response.sendRedirect("cropping");
return;
}
凡
private void createPhoto(InputStream is, String outputpath) {
FileOutputStream os = null;
try {
os = new FileOutputStream(outputpath);
// write bytes taken from uploaded file to target file
int ch = is.read();
while (ch != -1) {
os.write(ch);
ch = is.read();
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
Helpers.close(os);
}
}
現在發生的事情是,該文件在StoreLocation(???)上提交表格,以便上傳顯然這一切p1.getInputStream()
是化爲烏有。
我的問題是:
- 是什麼StoreLocation? tmp如何上傳這些glassfish?所有這些參數在哪裏設置?我確實讀過BalusC'tutorial - 但沒有提及StoreLocation(谷歌是不是很有幫助either)。
- 什麼是處理這種情況的更專業的方式 - 包括將照片保存在webroot之外 - 但使用glassfish提供的工具(如果它提供的話)?
- 即使P1印刷這麼好我想不起來了(它不seem重寫
toString()
)
有興趣的提示,即使在一個人應該如何命名的相片等(這是sessionID的東西吧? - 還檢查的時候招):
if (request.getParameter("save") != null) {
long time = System.currentTimeMillis();
String path = "images/upload/" + session.getId() + time + ".jpg";
String outputpath = this.getServletContext().getRealPath(path);
// store photo
InputStream is = p1.getInputStream();
createPhoto(is, outputpath);
// etc
}
什麼是StoreLocation類的完全限定名?我看起來不熟悉 – gerrytan 2013-09-30 03:49:40
@gerrytan:不是課堂 - 只是'Parts'的toString()中的一個打印輸出 - 將很快更新問題 - 我想我可以回答一些問題 - 我太忙了時刻 – 2013-09-30 10:06:03