Grails:编辑(表单:multipart)文件(来自客户端)并将其发送回客户端

时间:2016-01-20 07:29:55

标签: grails fileinputstream

这是我需要做的。

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强制转换为File。我想知道这段代码是否有希望。

是否可以修改Multipartfile并将其发送回客户端而不将文件保存到服务器,或者我是否真的需要先将其保存到服务器以便我可以做我需要做的事情?如果有可能,我该怎么办?什么是最好的方法呢?

1 个答案:

答案 0 :(得分: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();
}