2012-03-19 4 views
1

私は豆でこのような機能を持っている:私のアプリケーション内のすべてのファイルがjboss tmpディレクトリに書き込まれるのはなぜですか?

public String uploadFile(UploadedFile uploadedFile) { 
    logger.info("Enter: uploadFile(UploadedFile uploadedFile)."); 
    String name = uploadedFile.getName(); 
    String extension = name.substring(name.length() - 3); 
    if (extension.contentEquals("peg")) { 
     extension = "jpeg"; 
    } 

    RandomString rs = new RandomString(RANDOM_PHOTO_NAME_LENGTH); 
    this.randomPhotoName = rs.nextString(); 

    String fileName = this.randomPhotoName + "." + extension; 
    logger.info("File name: " + name + ". Extension: " + extension + ". New fileName: " + fileName); 
    ServletContext sc = (ServletContext) FacesContext.getCurrentInstance() 
      .getExternalContext().getContext(); 

    File f = new File(
      sc.getRealPath(Constant.USER_FILE_PATH)); 
    if (!f.exists()) { 
     logger.info("Folder " 
       + Constant.USER_FILE_PATH 
       + " nie istniej. Tworze nowy."); 
     f.mkdirs(); 
    } 
    File backupFile = new File(
      sc.getRealPath(Constant.USER_FILE_PATH 
        + fileName)); 

    InputStream in = null; 
    OutputStream out = null; 
    try { 
     in = new BufferedInputStream(uploadedFile.getInputStream()); 
     out = new FileOutputStream(backupFile); 

     byte[] buf = new byte[1024]; 
     int len; 
     while ((len = in.read(buf)) > 0) 
      out.write(buf, 0, len); 
    } catch (IOException ioe) { 
     FacesContext context = FacesContext.getCurrentInstance(); 
     FacesMessage msg = null; 
     msg = new FacesMessage(
       "Pojawił się nieoczekiwany błąd przy uploadowaniu pliku."); 

     context.addMessage(null, msg); 
     logger.error(
       "Pojawił się nieoczekiwany błąd przy uploadowaniu pliku.", 
       ioe); 
    } finally { 
     try { 
      in.close(); 
      out.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    logger.info("Exit: uploadFile(UploadedFile uploadedFile)."); 
    return fileName; 
} 

を、すべてのファイルがtmpディレクトリの例で保存されます。

\jboss-5.1.0.GA\server\default\tmp\a006-czo4uq-gzzu4l42-1-gzzuk7xr-a2\TupTus.war\media\img\user-gallery\6u2fpgu3tkzniwg.JPG 

答えて

4

すべてのファイルは以下のように構築されているので:

File backupFile = new File(
      sc.getRealPath(Constant.USER_FILE_PATH 
        + fileName)); 

sc.getRealPath()のように聞こえると、JBossがアプリケーションに割り当てる作業ディレクトリが返されます。

実際の質問はあなたにあります。ファイルはどこに書きたいのですか?そこにいなければ、どこで?ユーザーの一時ディレクトリの使用を希望する場合はnew File(System.getProperty("java.io.tmpdir"), fileName)と書いてください。

ボックスからパスを構成したい場合は、このパスをDBまたは構成ファイルに格納するか、コマンドラインスイッチ-Dを使用してJBossを実行しているときにカスタムシステムプロパティを使用してパスを渡すことができます。

+0

このファイルをjbossディレクトリに書きます:\ jboss-5.1.0.GA \ server \ default \ deploy \ TupTus.war \ media \ img \ user-gallery \ 6u2fpgu3tkzniwg.JPG –

関連する問題