2012-07-23 27 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