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()都是p1.getInputStream()

我的问题是:

  • 什么是StoreLocation? 那些glassfish上传的tmp怎么样? 这些参数设置在哪里? 我确实阅读了BalusC的教程 – 但没有提到StoreLocation(谷歌也不是很有帮助)。
  • 处理这种情况的更专业的方法是什么 – 包括将照片保存在webroot之外 – 但是使用glassfish提供的设施(如果确实提供)?
  • 即使是p1打印这么好逃脱了我(它似乎没有Override toString()

即使在如何重命名照片等方面也感兴趣(这个会话ID是对的吗? – 还检查时间技巧):

 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 } 

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

您还可以使用java系统propeties查找特定于环境的路径: user.diruser.home等。请参阅Java SE Tutorial上的系统属性 。 或者使用glassfish相对路径,请参阅glassfish系统属性 。

一旦你引用了Part ,它只是做文件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以方便使用,但可以随意编写自己的复制方法。 您还应该添加exception处理方法

什么是StoreLocation? 那些glassfish上传的tmp怎么样? 这些参数设置在哪里?

StoreLocation只是FileItem数据在磁盘上临时位置的java.io.File对象。 驻留在javax.servlet.context.tempdir ,默认为%GLASSFISH_HOME%\domains\domain1\generated\jsp\webApp 。 这些上传与tmp一样( 文件的生命周期与FileItem实例的生命周期相关;当实例被垃圾收集时,文件将被删除 – 从这里开始 )。 还没有设法以编程方式更改javax.servlet.context.tempdir的值(请注释) – 它是sun-web.xml的sun-web-app元素的tempdir属性。

处理这种情况的更专业的方法是什么 – 包括将照片保存在webroot之外 – 但是使用glassfish提供的设施(如果确实提供)?

更专业的方法是使用Part.write() 文件移动到所需的位置。 由于glassfish的实现,你无法提供绝对的写作路径 – 一件苦差事。 我在这问。

至于保存文件的位置: https : //stackoverflow.com/a/18664715/281545

这是为了保存文件 – 从应用程序外部的位置为您提供服务,您需要在sun-web.xml(或glassfish-web.xml)中定义“alternatedocroot”属性。

即使是p1打印这么好逃脱了我(它似乎没有Override toString())

哦,是的确如此

即使在如何重命名照片等方面也感兴趣(这个会话ID是对的吗? – 还检查时间技巧)

不,不是 – 我倾向于File#createTempFile() – 无论如何这是一个不同的问题