1

I am currently working on an application, where users are given an option to browse and upload excel file, I am badly stuck to get the absolute path of the file being browsed. As location could be anything (Windows/Linux).

import org.apache.myfaces.custom.fileupload.UploadedFile;
-----
-----
private UploadedFile inpFile;
-----
getters and setters    
public UploadedFile getInpFile() {
    return inpFile;
} 
@Override
public void setInpFile(final UploadedFile inpFile) {
    this.inpFile = inpFile;
}

we are using jsf 2.0 for UI development and Tomahawk library for browse button.

Sample code for browse button

t:inputFileUpload id="file" value="#{sampleInterface.inpFile}" 
        valueChangeListener="#{sampleInterface.inpFile}" />

Sample code for upload button

     <t:commandButton action="#{sampleInterface.readExcelFile}" id="upload" value="upload"></t:commandButton>

Logic here

Browse button -> user will select the file by browsing the location Upload button -> on Clicking upload button, it will trigger a method readExcelFile in SampleInterface.

SampleInterface Implementation File

public void readExcelFile() throws IOException {

        System.out.println("File name: " + inpFile.getName());
    String prefix = FilenameUtils.getBaseName(inpFile.getName()); 
    String suffix = FilenameUtils.getExtension(inpFile.getName());
        ...rest of the code
            ......
 }

File name : abc.xls

prefix : abc

suffix: xls

Please help me in getting the full path ( as in c:.....) of the file being browsed, this absolute path would then be passed to excelapachepoi class where it will get parsed and contents would be displayed/stored in ArrayList.

S Jagdeesh
  • 1,523
  • 2
  • 28
  • 47

2 Answers2

3

Why do you need the absolute file path? What can you do with this information? Creating a File? Sorry no, that is absolutely not possible if the webserver runs at a physically different machine than the webbrowser. Think once again about it. Even more, a proper webbrowser doesn't send information about the absolute file path back.

You just need to create the File based on the uploaded file's content which the client has already sent.

String prefix = FilenameUtils.getBaseName(inpFile.getName()); 
String suffix = FilenameUtils.getExtension(inpFile.getName());
File file = File.createTempFile(prefix + "-", "." + suffix, "/path/to/uploads");

InputStream input = inpFile.getInputStream();
OutputStream output = new FileOutputStream(file);

try {
    IOUtils.copy(input, output);
} finally {
    IOUtils.closeQuietly(output);
    IOUtils.closeQuietly(input);
}

// Now you can use File.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for a wonderful and to the point explanation BalusC. There must have been a major misunderstanding on my part. My logic was when the user browse the file and click on upload button. It will trigger readexcel() function via interface which then send the file full path to the ExcelApache java class where the file will be read and parsed. fileName =new POIFSFileSystem(new FileInputStream(file)); – S Jagdeesh Dec 10 '11 at 04:57
  • 1
    You can also just use `new POIFSFileSystem(inpFile.getInputStream())` if you don't care about having a copy of the file on the disk. – BalusC Dec 10 '11 at 05:02
  • that should work on both, windows as well as on unix file system? the application has an option where the users would be asked to upload the excel/word document file, the user could be on any platform. I am just clarifying it. – S Jagdeesh Dec 11 '11 at 18:13
  • 1
    Yes. It's after all just a byte stream. – BalusC Dec 11 '11 at 19:51
0

I remember to have some problem with this in the past too. If I am not mistaken, I think you cannot get the full file path when uploading a file. I think the browser won't tell you it for security purposes.

Averroes
  • 4,168
  • 6
  • 50
  • 63
  • Thanks for the reply Averroes, My actual purpose was to read excel file using poi library. As location is being decided on the run time on browse button, my understanding was to send the full absolute path to read the file. seems my "LOGIC" lacks the "Logic" here :) – S Jagdeesh Dec 10 '11 at 05:03