【问题标题】:get directory of uploaded file java获取上传文件java目录
【发布时间】:2014-11-05 16:00:10
【问题描述】:

我正在尝试上传一个 Excel 文件并阅读它。当我提供绝对路径时,我可以读取文件,但在上传时不想工作。我尝试了不同的东西,但找不到解决方案。 我没有使用 jsp 和 servlet,而是使用 html/controller/service

html页面

<h:form enctype="multipart/form-data">
        <h:panelGrid>
            <h:outputLabel value="Upload file: " style="font-weight:bold" />
            <h:inputFile  value="#{uploadController.path}" />

            <h:commandButton value="Excel lezen en opslaan" action="#    {uploadController.excelOpslaan}"/>
        </h:panelGrid>
</h:form>

控制器

 private String path;


 public String getPath() {
    return path;
 }

 public void setPath(String path) {
    this.path = path;
 }

 public void excelOpslaan(){

    PuntenService.getExcel(path);
 }

服务

 public List<String> getExcel(String path);

服务实现

private List<String> list = new ArrayList<String>();

@Override 
public List<String> getExcel(String path){

    FileInputStream file = null;
    OPCPackage pkg = null;



    try {

        file = new FileInputStream(path);
        // create a new OPC Package to obtain a workbook
        pkg = OPCPackage.open(file);

        //Create Workbook instance holding reference to .xlsx file 
        XSSFWorkbook workbook = new XSSFWorkbook(pkg);

        //Get first/desired sheet from the workbook 
        XSSFSheet sheet = workbook.getSheetAt(0);

        //Iterate through each rows one by one 
        Iterator<Row> rowIterator = sheet.iterator();


        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            //For each row, iterate through all the columns 

            if (row.getRowNum() <= 5) {
            continue;// skip to read the first 7 row of file
            }


            Iterator<Cell> cellIterator = row.cellIterator();

            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();

                 switch (cell.getCellType()) 
                {
                    case Cell.CELL_TYPE_STRING:
                        list.add(cell.getStringCellValue());
                        break;
                }

            }
            //System.out.println("");
        }
        file.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return list;


}

【问题讨论】:

    标签: java file upload path controller


    【解决方案1】:

    我不会深入你的程序,但尝试使其适应 java7+ 标准(例如,将路径存储为路径,而不是字符串),也许它会有所帮助..

    一些例子

     List<String> readSmallTextFile(String aFileName) throws IOException {
        Path path = Paths.get(aFileName);
        return Files.readAllLines(path, ENCODING);
      }
    
      //For larger files
    
      void readLargerTextFile(String aFileName) throws IOException {
        Path path = Paths.get(aFileName);
        try (Scanner scanner =  new Scanner(path, ENCODING.name())){
          while (scanner.hasNextLine()){
            //process each line in some way
            log(scanner.nextLine());
          }      
        }
      }
    
      void readLargerTextFileAlternate(String aFileName) throws IOException {
        Path path = Paths.get(aFileName);
        try (BufferedReader reader = Files.newBufferedReader(path, ENCODING)){
          String line = null;
          while ((line = reader.readLine()) != null) {
            //process each line in some way
            log(line);
          }      
        }
      }
    

    来源 http://www.javapractices.com/topic/TopicAction.do?Id=42

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-31
      • 1970-01-01
      • 1970-01-01
      • 2021-11-20
      • 2019-10-08
      • 2011-06-07
      相关资源
      最近更新 更多