0

I am trying to get my JSF site to upload a picture to the server, but am having a time of it. I've found 4 methodologies to do, but I'd like to use h:InputFile as it seems the most direct.

It would seem I just need to supply the upload path correctly.

After adding @MultipartConfig I no longer get an exception, but I can't verify the file is uploaded or see any error.

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;
}
}

Hopefully I've copied the necessary information correctly.

Kamurai
  • 143
  • 2
  • 4
  • 19
  • Please read this topic: http://stackoverflow.com/questions/27677397/how-to-upload-file-using-jsf-2-2-hinputfile-where-is-the-saved-file – tam nguyen Apr 27 '17 at 04:41
  • Thank you Tam, that is one of the many articles I had found. Turns out I had nested form issue that was interfering with this, I'll update the question accordingly, but now (it would seem) it is just a matter of supplying a correct pathway for the file. – Kamurai Apr 27 '17 at 15:11

1 Answers1

0

After going back and rereading all the articles I had bookmarked, it was actually the from the one Tam had suggested that I was able to strip out some information.

I didn't need the AJAX, or the @MultipartConfig, and my previous attempt was somehow incorrect, but the follow method allowed me to successfully upload a picture where I wanted it:

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;
}
Kamurai
  • 143
  • 2
  • 4
  • 19