2012-07-23 39 views
4

我是最新的glassfish(3.1.2) - 所以不需要Apache的FileItem,也没有getPart()的错误。我读过上传图像的最佳做法是将它们保存在文件系统上(例如参见here)。我编辑现有的代码 - 臭那个 - 所以我有想法做:Glassfish - 上传图片 - 正确地做

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()是化为乌有。

我的问题是:

  • 是什么StoreLocation? tmp如何上传这些glassfish?所有这些参数在哪里设置?我确实读过BalusC'tutorial - 但没有提及StoreLocation(谷歌是不是很有帮助either)。
  • 什么是处理这种情况的更专业的方式 - 包括将照片保存在webroot之外 - 但使用glassfish提供的工具(如果它提供的话)?
  • 即使P1印刷这么好我想不起来了(它不seem重写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 
} 
+0

什么是StoreLocation类的完全限定名?我看起来不熟悉 – gerrytan 2013-09-30 03:49:40

+0

@gerrytan:不是课堂 - 只是'Parts'的toString()中的一个打印输出 - 将很快更新问题 - 我想我可以回答一些问题 - 我太忙了时刻 – 2013-09-30 10:06:03

回答

0

什么是StoreLocation? tmp如何上传这些glassfish?所有这些参数在哪里设置?

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

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

好更专业的方法是使用Part.write()移动文件到所需的位置。由于glassfish的实现,尽管你不能提供绝对路径来写 - 一件杂事。我问here

在何处保存文件:https://stackoverflow.com/a/18664715/281545

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

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

哦,是it does

有兴趣的提示,即使在一个人应该如何命名的相片(这是sessionID的事情吗? - 检查时间的把戏)

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

0

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

您还可以使用java系统propeties查找环境特定路径:user.dir,user.home等请参阅System Properties on Java SE Tutorial。或者使用glassfish相对路径,请参阅glassfish system properties

一旦你有参考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为了方便而随便写自己的副本方法。您还应该添加异常处理方法

+0

看看我的答案 - 我主要关心的是移动(而不是_copy_)该文件 - 并找出所有那些glassfish cruft是什么 – 2013-11-02 16:04:37