How to get the uploaded file path in JSF

ぐ巨炮叔叔 提交于 2020-01-25 23:03:22

问题


I'm importing Excel document for reading and displaying the content in the UI. I need to know how to get the path of the file uploaded using browse in order to read the content of the excel file.


回答1:


I need to know how to get the path of the file uploaded using browse in order to read the content of the excel file.

There's a major thinking mistake here.

Imagine that I am the client who want to upload the file and that you are the server who need to get the file's contents. I give you the path c:\path\to\foo.xls as sole information. How would you as being the server ever get its contents? Do you have an open TCP/IP connection to my local hard disk file system? Really? Is everyone else's local disk file system with all that sensitive information really so easily accessible by Internet?

That isn't how uploading files works. This would only ever work if the server runs at physically the same machine as the client, so that you can just use FileInputStream with that path (which would only occur in local development environment).

You should instead be interested in the sole file contents which the client has sent to you along with the HTTP request body. In HTML terms, you can use <input type="file"> for this. But until the upcoming JSF 2.2 there is no equivalent standard JSF <h:xxx> component for this. You need to look for JSF component libraries offering a component for this. In your case, with RichFaces (as tagged in your question), you can use <rich:fileUpload> for this. It's unclear which RichFaces version you're using, but for RF 3.3.3 you can find a complete example in the 3.3.3 showcase site and for RF 4.0 in the 4.0 showcase site.

Whatever way you choose, you should in your JSF managed bean ultimately end up with a byte[] or an InputStream representing the file contents. Then you've all the freedom to store it wherever you want on the server's local disk file system using for example FileOutputStream. You can even also just feed it directly to whatever Excel API you're using, most of them have just a method taking an InputStream or byte[].




回答2:


you better take a look at this article. The solution that uses Tomahawk 2.0 ( http://myfaces.apache.org/tomahawk-project/tomahawk20/index.html ) by BalusC is great

JSF 2.0 File upload

Everything you need is there




回答3:


String fileName = FilenameUtils.getName(uploadedFile.getName());
byte[] bytes = uploadedFile.getBytes();
FileOutputStream outputStream = null;
try {
    String filePath = System.getProperty("java.io.tmpdir") + "" + fileName;
     outputStream = new FileOutputStream(new File(filePath));
     outputStream.write(bytes);
     outputStream.close();
     readExcelFile(filePath);
} catch (Exception ex) {

}
In the above code Iam uploading the file using tomahawk after uploading storing the      file in a temporary location.And from there i will be reading using  poi.                                                                                           
public static void readExcelFile(String fileName)
{ 
    try{
      FileInputStream myInput = new FileInputStream(fileName);
       POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
       org.apache.poi.ss.usermodel.Workbook workbook = WorkbookFactory.create(myFileSystem);
        org.apache.poi.ss.usermodel.Sheet sheet  = workbook.getSheetAt(0); 
         Iterator rowIter = sheet.rowIterator();  
          for(Row row : sheet) {
          //u can read the file contents by iterating.
          }
    } catch (Exception ex) {

    }

}   


来源:https://stackoverflow.com/questions/12314003/how-to-get-the-uploaded-file-path-in-jsf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!