2017-04-26 56 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; 
}