Glassfish的 - 上传图片 - 这样做的权利(Glassfish - uploading i

2019-07-30 13:57发布

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);

Prints :

!!!!!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;
}

Where

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);
    }
}

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.

My questions are :

  • 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())

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
}

Answer 1:

良好做法是选择在哪里照片将上传文件系统的路径。 通常这种路径被编程为通过java系统属性进行配置(例如:通过使-Dcom.mycompany.uploadPath=/path/to/photos/dir上JVM参数系统属性)。

您还可以使用Java系统化子性质发现环境的具体路径: user.diruser.home等请参见上的Java SE教程系统属性 。 或者使用GlassFish的相对路径,请参阅GlassFish的系统性能 。

一旦你有参考部分 ,它只是在做文件IO上载的文件复制到该上传的路径,例如:

Part part = // obtain part somehow..
String photoFileName = // build a file name somehow..
InputStream photoInputStream = part.getInputStream();
FileOutputStream photoOutputStream = new FileOutputStream(System.getProperty("com.mycompany.uploadPath") + File.separator + photoFileName);
IOUtils.copy(photoInputStream, photoOutputStream);
// close streams here...

上面的代码使用Apache的IOUtils为了方便而随意编写自己的复制方法。 您还应该添加异常处理方法



Answer 2:

什么是StoreLocation? 如何TMP是那些GlassFish的上传? 哪里都是那些参数设置?

StoreLocation是只为在该java.io.File的对象FileItem的数据的磁盘上的临时位置。 驻留在javax.servlet.context.tempdir缺省值为%GLASSFISH_HOME%\domains\domain1\generated\jsp\webApp 。 这些上传是作为TMP的任何东西( 文件的寿命是联系在一起的寿命FileItem实例;当实例作为垃圾回收的文件将被删除 -从这里 )。 现在还没有设法改变的值javax.servlet.context.tempdir编程(建议请) -这是tempdir的财产阳光web-app元素的sun-web.xml中的。

这将是一个更专业的处理情况的方式 - 包括保持外根目录的照片 - 但使用设施GlassFish提供(如果它确实提供)?

那么一个更专业的方法是使用Part.write()将文件移动到所需的位置。 由于GlassFish的实施虽然你不能提供写绝对路径 - 一个苦差事。 我问这里 。

至于在哪里保存文件: https://stackoverflow.com/a/18664715/281545

这是用于保存文件 - 从您需要定义在sun-web.xml中(或与GlassFish的web.xml)“alternatedocroot”属性中的应用程序以外的位置为它服务。

即使P1印刷这么好我想不起来了(这似乎并没有覆盖的toString())

噢,是它

有兴趣的提示,即使在一个应该如何命名的相片等(这是sessionID的东西吧? - 还检查的时候招)

不,它不是-我对倾向于File#createTempFile() -反正这是一个不同的问题问在这里



文章来源: Glassfish - uploading images - doing it right