How to change getRealPath() save location? Photocam primefaces

Asked

Viewed 74 times

0

I’m trying to use the Photocam component of Primefaces, it’s working, but the image is saved within the target of the application. I want to move the rescue site. Follows an excerpt from the code.

   ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    String newFileName = externalContext.getRealPath("") + File.separator + "images" + File.separator + "photocam" + File.separator + filename + ".jpeg";

I really appreciate it if someone can help me.

1 answer

1


  • 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

  • Mark as sure the answer and vote on it, to index and help other people

Browser other questions tagged

You are not signed in. Login or sign up in order to post.