2016-01-20 129 views
0

这就是我需要做的。Grails:编辑(form:multipart)文件(从客户端)并将其发送回客户端

1) Accept an xlsx/xls file from client. 
2) Backend will receive it in the form of multipart file 
3) The file will be processed and if the format of the data is invalid, that same file will be updated and the error message will be written in the side of the input of the client. 
4) this modified file will be sent back to the user. 

到目前为止,这是我做了什么。

def generateErrorReport(ServletResponse response, Map messageCollections, MultipartFile file, String ext){ 

    FileInputStream fileIn = file.getInputStream() 
    Workbook workbook = (ext.equalsIgnoreCase("xls")) ? new HSSFWorkbook(fileIn) : new XSSFWorkbook(fileIn) 

    workbook = this.getWorkbook((MultipartFile) file, ext.equalsIgnoreCase("xls")); 
    try { 
     Sheet sheet = workbook.getSheetAt(0) 
     Long lastCellNum = sheet.getRow(0).getLastCellNum(); 

     for(int i=1; i<sheet.getLastRowNum(); i++){ 
      if(messageCollections[i]!=null && messageCollections[i]!=[]) { 
       Cell cell = sheet.getRow(i).getCell(lastCellNum + 1) 
       cell.setCellValue(messageCollections[i]); 
      } 
     } 

     fileIn.close() 

     FileOutputStream fileOut = new FileOutputStream((File) file) 
     workbook.write(fileOut); 

     response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") 
     response.setHeader("Content-Disposition", "Attachment;Filename=error.xlsx") 
     response.outputStream << fileOut 
     response.outputStream.flush() 

     fileOut.close() 
    }catch(Exception ex){ 
     println ex 
    } 
} 

此代码无法正常工作,因为您无法将MultipartFile转换为文件。我想知道这个代码是否还有希望。

是否有可能修改Multipartfile,并将其发送回客户端不保存文件到服务器或者我真的需要它首先保存到服务器,所以我可以做我需要做什么?如果可能的话,我该怎么做?什么是最好的方法来做到这一点?

+0

简短的回答是肯定的。一旦从request.getFile(“”)获得multiPartFile,您可以修改文件并将其输出到用户。没有必要保存文件。什么是最好的方法?答案取决于用例,你想修改的文件的数量,加载你的应用程序等。 – Armaiti

+0

@Aramiti你能告诉我该怎么做?谢谢! – user3714598

+0

这可能会帮助你:[将MultipartFile转换为java.io.File而不复制到本地机器](http://stackoverflow.com/questions/21089106/converting-multipartfile-to-java-io-file-without-copying -to-LOCAL-MACHINE) – Armaiti

回答

0

这解决了我的问题

private void createReport(ServletResponse response, Map message, MultipartFile file, String ext){ 
     InputStream is = file.getInputStream(); 
     OutputStream os = response.outputStream; 

     String fileName = "desiredFilename." + ext 

     response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); 
     response.setHeader("Content-Disposition", "Attachment;Filename=${fileName}"); 

      PoiTransformer transformer = PoiTransformer.createTransformer(is, os); 
      org.apache.poi.ss.usermodel.Workbook workbook = transformer.getWorkbook() 
      Sheet sheet = workbook.getSheetAt(workbook.getActiveSheetIndex()) 
      int lastColNum = sheet.getRow(0).getLastCellNum() 

      Cell cell; 

      cell = sheet.getRow(0).getCell(lastColNum); 
      if(cell==null){ 
       cell = sheet.getRow(0).createCell(lastColNum); 
      } 
      cell.setCellType(1) 
      cell.setCellValue("Message") 
      cell.setCellStyle(getStyle(workbook, 2)) 

      for(int it=1; it<sheet.getLastRowNum(); it++) { 
       if (message.get(new Long(it))!=null && message.get(new Long(it))!=[]) { 
        cell = sheet.getRow(it).getCell(lastColNum); 
        if(cell==null){ 
         cell = sheet.getRow(it).createCell(lastColNum); 
        } 
        cell.setCellType(1) 
        cell.setCellValue(message.get(new Long(it)).join(', ')) 
        cell.setCellStyle(getStyle(workbook, 1)) 
       } 
      } 

      sheet.autoSizeColumn(lastColNum); 
      transformer.write(); 
} 
相关问题