Convert org.apache.myfaces.trinidad.model.UploadedFile to java.io.File
// assume that you have the UploadedFile object named uploadFile
InputStreamReader reader = new InputStreamReader(uploadFile.getInputStream());
int partition = 1024;
int position = 0;
int length = 0;
char[] buffer = new char[partition];
FileWriter fstream = new FileWriter("test.tmp");
do{
length = reader.read(buffer, position, partition)
fstream.write(buffer, position, length);
}while(length > 0);
File file = new File("test.tmp");
/**
* Read the attached file content as a byte[] and convert to Datahandler
* @param valueChangeEvent
*/
public void onFileUpload(ValueChangeEvent valueChangeEvent) {
String methodName = "onFileUpload(ValueChangeEvent)";
_logger.entering(CLAZZ_NAME, methodName);
try {
UploadedFile file = (UploadedFile)valueChangeEvent.getNewValue();
byte[] byteArray=org.apache.commons.io.IOUtils.toByteArray(file.getInputStream());
DataHandler dataHandler = new DataHandler(byteArray,"application/octet-stream");
} catch (Exception b64de) {
_logger.logp(Level.SEVERE, CLAZZ_NAME, methodName,
" Exception Occured", b64de);
b64de.printStackTrace();
}
_logger.exiting(CLAZZ_NAME, methodName);
}
Hi i have use this method to convert UploadedFile into File object, by all i get is empty file at the end. plz can u tell me wat i am doing wrong here is my code for this.
ReplyDeletepublic File convertUploadedFileIntoFileObject(UploadedFile uploadedFile) {
// assume that you have the UploadedFile object named uploadedFile
int partition = 1024;
int length = 0;
int position = 0;
char[] buffer = new char[partition];
// uploadedFile.getOpaqueData();
try {
InputStreamReader reader = new InputStreamReader(uploadedFile.getInputStream());
FileWriter fstream = new FileWriter("out.tmp");
do {
length = reader.read(buffer, position, partition);
if (length > 0)
fstream.write(buffer, position, length);
} while (length > 0);
File file = new File("out.tmp");
return file;
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}