我正在使用最新的Glassfish(3.1.2),因此不需要使用Apache FileItem,也没有使用getPart()时出现的错误。我读到上传图像的最佳实践是将它们保存在文件系统中(例如请参见这里)。我正在编辑已经存在的代码 - 而且还很臭 - 所以我想做以下操作:
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
newlines mine. In the code people are doing :
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(???),所以所有这些都是无用的。
我的问题是:
- 什么是StoreLocation?这些glassfish上传有多长时间?所有这些参数都在哪里设置?我确实阅读了BalusC的tutorial - 但没有提到StoreLocation(Google也不是很有帮助either)。 - 处理这种情况的更专业的方法是什么 - 包括将照片保留在Webroot之外 - 但使用glassfish提供的设施(如果它确实提供)? - 即使p1打印得很好,也难以理解(它似乎没有覆盖
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
}
Parts
的 toString() 中打印输出 - 我很快会更新问题 - 我认为我可以回答其中的一些问题 - 我现在只是太忙了。 - Mr_and_Mrs_D