Grails:编辑(形式:多部分)文件(来自客户端)并将其发送回客户端

Grails : Edit (form: multipart) file (from client) and send it back to the client

这就是我需要做的。

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 并将其发送回客户端而不将文件保存到服务器,或者我真的需要先将它保存到服务器以便我可以做我需要做的事情吗?如果可能的话,我该怎么做?最好的方法是什么?

这解决了我的问题

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();
}