2017-04-26 95 views
0

我想讓我的JSF網站上傳圖片到服務器,但我有一個時間。我發現了4種方法,但我想使用h:InputFile,因爲它似乎是最直接的。上傳JSF文件(需要正確的文件路徑)

看來我只是需要正確提供上傳路徑。

添加@MultipartConfig後,我不再收到異常,但無法驗證文件是否已上傳或看到任何錯誤。

public void AddPicture() 
{ 
    ConnInfo HitIt = new ConnInfo(); 

    try 
    { 
     HitIt.save(fileCelebrityToAdd); 
    } 
    catch(Exception ex) 
    { 
     //? 
    } 
} 




@MultipartConfig(location="C:\\local\\pathway\\Netbeans\\project\\web\\Pictures\\items\\") 
public class ConnInfo 
{ 
private String uploadLocation; 

public ConnInfo() 
{ 
    //uploadLocation = ".\\Pictures\\items\\"; 
    uploadLocation = "C:\\local\\pathway\\Netbeans\\project\\web\\Pictures\\items\\"; 
} 

public boolean TryOut(Part file) throws IOException 
{ 
    String monkey = uploadLocation+getFilename(file); 

    try 
    { 
     file.write(monkey); 
    } 
    catch(Exception ex) 
    { 
     return false; 
    } 

    return true; 
} 
} 

希望我已經正確地複製了必要的信息。

+0

請仔細閱讀本主題:http://stackoverflow.com/questions/27677397/how-to-upload-file-using-jsf-2-2-hinputfile-where-is-the-saved-file –

+0

謝謝譚,這是其中之一我發現的很多文章。事實證明,我嵌套的表單問題是干擾這一點,我會相應地更新問題,但現在(看起來)它只是提供一個正確的文件路徑的問題。 – Kamurai

+0

不客氣:) –

回答

0

回去後,重新閱讀我收藏的所有文章,實際上是從譚建議我能夠去掉一些信息。

我並不需要AJAX,或@MultipartConfig,和我以前的嘗試是不正確莫名其妙,但後續的方法讓我成功地上傳圖片,我想它:

public boolean SaveHer(Part file) 
{ 
    String monkey = getFilename(file); 

    try (InputStream input = file.getInputStream()) 
    { 
     Files.copy(input, new File(uploadLocation, monkey).toPath()); 
    } 
    catch (IOException e) 
    { 
     // Show faces message? 
     return false; 
    } 
    return true; 
}