Glassfish - загрузка изображений - все правильно
Я использую последнюю версию 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
новая строка моя. В коде люди делают:
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()
это ни для чего.
Мои вопросы:
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 OverridetoString()
)
Интересуются советами даже о том, как следует переименовать фотографии и т. Д. (Правильно ли этот 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
}