Prime Faces Image File Upload

UI Components for JSF
Post Reply
snehakarthik
Posts: 5
Joined: 14 Jan 2019, 13:00

27 Feb 2019, 08:24

Hi Team,

I am using <p:fileupload> tag in my jsf pages. I am trying to fetch the path of the file uploaded from my jsf page but unable to fetch it.
So i have done a work around where in I am copying the contents of the file uploaded into a new file (created in the local computer as temp file) and making use of that path. Could you please suggest a better solution for this?

My code is as follows:

public void handleFileUpload(FileUploadEvent event) {
if (event != null) {
UploadedFile merchantImageFile = event.getFile();
File tempMerchantImageFile = null;
try {
tempMerchantImageFile = File.createTempFile("merchant_image_", ".tmp");// prefix and suffix(paramters) for the temporary file.
FileOutputStream fileOutputStream = new FileOutputStream(tempMerchantImageFile);
InputStream fileInputStream = merchantImageFile.getInputstream();
int BUFFER_SIZE = 8192;
byte[] buffer = new byte[BUFFER_SIZE];
int a;
while (true) {
a = fileInputStream.read(buffer);
if (a < 0)
break;
fileOutputStream.write(buffer, 0, a);
fileOutputStream.flush();
}
fileOutputStream.close();
fileInputStream.close();
merchantImageFilePath = tempMerchantImageFile.getAbsolutePath();
addMessage("Merchant Image loaded succesfully.");
} catch (IOException e) {
logger.debug(e.getMessage());
addMessage("Merchant Image Content fetching failed.");
}
}
}

kukeltje
Expert Member
Posts: 9606
Joined: 17 Jun 2010, 13:34
Location: Netherlands

05 Mar 2019, 11:40

Please read the 'PLEASE PLEASE...' topic in the forum
- format your code using code tags
- Make it a real MCVE (see google)
- Post version info (PrimeFaces AND jsf version and impl)

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: HessiJames and 23 guests