我是最新的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