- First you need to read the file
- Second you need to know where you want to save the new file
- Third party you need to copy or crop the file to the new location
- I like to use apache lib, Fileutils is simple and easy to use
Below is a method that I use to do this
public static File changeUploadedFileInFile(UploadedFile fileOld, String pathSaveFile) throws IOException
{
File file = new File(pathSaveFile);
OutputStream outputStream = null;
InputStream input = null;
try
{
FileUtils.forceMkdir(file);
file = new File(pathSaveFile + fileOld.getFileName());
outputStream = new FileOutputStream(file);
input = fileOld.getInputstream();
int read = 0;
byte[] bytes = new byte[1024];
while ((read = input.read(bytes)) != -1)
{
outputStream.write(bytes, 0, read);
}
return file;
}catch (IOException e)
{
LOG.error(SysUtilLogLevel.ERROR, "Error close stream file importer", e);
throw new IOException();
}
finally
{
if (outputStream != null)
{
try
{
outputStream.close();
}
catch (IOException e)
{
LOG.error(MarkerFactory.getMarker("error"), "Error close stream file importer", e);
}
}
if (input != null)
{
try
{
input.close();
}
catch (IOException e)
{
LOG.error(MarkerFactory.getMarker("error"), "Error close stream file importer", e);
}
}
This method is creating a new file in pathSaveFile, and copying the old file into the new file. Do not delete the old file if it exists. but you even need to save can use Stream straight from memory if the files are not too large.
Cara worked, I did it as follows String newFileName = externalContext.getRealPath(".. .. src main webapp Resources algaworks images ") + File.separator + filename + ". jpeg"; Thanks
– Leandro Santos
Mark as sure the answer and vote on it, to index and help other people
– Marcelo Ferreira