gpt4 book ai didi

grails - Grails :(从客户端)编辑(形成:多部分)文件并将其发送回客户端

转载 作者:行者123 更新时间:2023-12-02 15:54:44 24 4
gpt4 key购买 nike

这就是我需要做的。

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并将其发送回客户端而不将文件保存到服务器,还是我真的需要先将其保存到服务器才能执行所需的操作?如果有可能,我该怎么办?最好的方法是什么?

最佳答案

这解决了我的问题

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

关于grails - Grails :(从客户端)编辑(形成:多部分)文件并将其发送回客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34893954/

24 4 0