从servlet文件上传文件时如何获取上传的文件路径?

时间:2017-04-05 05:08:08

标签: java servlets file-upload

这是我的代码(servlet)。

boolean isMultiPart = ServletFileUpload.isMultipartContent(request);
                    if (isMultiPart) {
                        ServletFileUpload upload = new ServletFileUpload();
                        try {
                            FileItemIterator itr = upload.getItemIterator(request);
                            while (itr.hasNext()) {
                                FileItemStream item = itr.next();
                                FILE_NAME = item.getName();

                                InputStream stream = item.openStream();
                                if (item.isFormField()) {
                                    String value = Streams.asString(stream, "UTF-8");
                                }
                            }
                            try {
                                FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
                                Workbook workbook = new XSSFWorkbook(excelFile);
                                Sheet datatypeSheet = workbook.getSheetAt(0);
                                Iterator<Row> iterator = datatypeSheet.iterator();
                            } catch (Exception adfasdasd) {
                                adfasdasd.printStackTrace();
                            }

                        } catch (FileUploadException ex) {
//                            Logger.getLogger(PropertyController.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (IOException exp) {
//                            Logger.getLogger(PropertyController.class.getName()).log(Level.SEVERE, null, exp);
                        }

FileInputStream excelFile = new FileInputStream(new File(FILE_NAME)); 要做到这一点,我需要上传文件的完整路径。我怎么得到它?

1 个答案:

答案 0 :(得分:0)

使用getPart的{​​{1}}方法。教程位于http://docs.oracle.com/javaee/6/tutorial/doc/glraq.html