Glassfish - 上传图片 - 做得对 [英] Glassfish - uploading images - doing it right
问题描述
我正在使用最新的glassfish(3.1.2) - 因此不需要apache FileItem,也不需要使用getPart()。我读过上传图片的最佳做法是将它们保存在文件系统中(参见这里例如)。我正在编辑已经存在的代码 - 很臭 - 所以我有想法:
I am on latest glassfish (3.1.2) - so no need for apache FileItem and no bugs with getPart(). I read that the best practice on uploading images is saving them on the file system (see here for instance). I am editing already existing code - smelly at that - so I had the idea to do :
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(???)在提交表单时显然所有这些 p1.getInputStream()
都是徒劳的。
Now what happens is that the file is uploaded in the StoreLocation (???) on submitting the form so apparently all this p1.getInputStream()
is for naught.
我的问题是:
- 什么是StoreLocation?那些glassfish上传的tmp怎么样?这些参数设置在哪里?我确实读过BalusC的'教程 - 但没有提到StoreLocation(谷歌不是很有帮助 ) 。
- 处理这种情况的更专业的方法是什么 - 包括将照片保存在webroot之外 - 但是使用glassfish提供的设施(如果确实提供)?
- 即使是p1打印这么好也逃过了我(它不是似乎覆盖
toString()
)
- what is StoreLocation ? How tmp are those glassfish uploads ? Where are all those parameters set ? I did read BalusC' tutorial - but there is no mention of StoreLocation (google is not very helpful either).
- What would be a more professional way of handling the situation - including keeping the photos outside the webroot - but using facilities glassfish provides (if it does provide) ?
- Even p1 printing so nice escapes me (it does not seem to Override
toString()
)
对于如何重命名照片等感兴趣的提示(这个会话ID是对的吗? - 还检查时间技巧):
Interested in tips even in how should one rename the photos etc (is this sessionID thing Right ? - check also the time trick) :
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?那些glassfish上传的tmp怎么样?所有这些参数设置在哪里?
What is StoreLocation ? How tmp are those glassfish uploads ? Where are all those parameters set ?
StoreLocation 就是java.io 。code> FileItem 的
数据在磁盘上的临时位置的.File对象。驻留在 javax.servlet.context.tempdir
,默认为%GLASSFISH_HOME%\ domain \ domain1 \ generated\jsp\webApp
。这些上传与tmp一样(文件的生命周期与 FileItem
实例的生命周期相关;当实例被垃圾收集时,文件将被删除 - 来自这里)。尚未设法以编程方式更改 javax.servlet.context.tempdir
的值(请注释) - 它是 tempdir sun-web-app元素的属性 sun-web.xml。
StoreLocation is just the the java.io.File object for the FileItem
's
data's temporary location on the disk. Resides in javax.servlet.context.tempdir
which defaults to %GLASSFISH_HOME%\domains\domain1\generated\jsp\webApp
. Those uploads are as tmp as anything (The lifetime of the file is tied to the lifetime of the FileItem
instance; the file will be deleted when the instance is garbage collected - from here). Haven't yet managed to change the value of javax.servlet.context.tempdir
programmatically (comment please) - it is the tempdir
property of the sun-web-app element of the sun-web.xml.
处理这种情况的更专业的方法是什么 - 包括保留照片在webroot之外 - 但是使用glassfish提供的设施(如果确实提供)?
What would be a more professional way of handling the situation - including keeping the photos outside the webroot - but using facilities glassfish provides (if it does provide) ?
更专业的方法是使用 Part.write()
将移动文件移至所需位置。由于glassfish的实现,你无法提供绝对的写作路径 - 一件苦差事。我问了这里。
Well a more professional way is to Use Part.write()
to move the file to the desired location. Due to glassfish implementation though you can't supply an absolute path to write - a chore. I asked here.
关于保存文件的位置: https://stackoverflow.com/a/18664715/281545
As to where to save the file : https://stackoverflow.com/a/18664715/281545
用于保存文件 - 从应用程序外部的位置提供服务,您需要在sun-web.xml(或glassfish-)中定义alternatedocroot属性web.xml)。
That is for saving the file - to serve it from a location outside the app you need to define "alternatedocroot" properties in the sun-web.xml (or glassfish-web.xml).
即使是p1打印这么好逃脱了我(它似乎没有覆盖toString())
Even p1 printing so nice escapes me (it does not seem to Override toString())
哦是的确实
感兴趣的提示甚至应该如何重命名照片等(这个sessionID是对的吗? - 检查时间tr ick)
Interested in tips even in how should one rename the photos etc (is this sessionID thing Right ? - check also the time trick)
不,不是 - 我倾向于文件#createTempFile()
- 无论如何这是一个不同的问题,请这里
No it is not - I tend towards File#createTempFile()
- anyway this is a different question asked here
这篇关于Glassfish - 上传图片 - 做得对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!