Как динамически загрузить файл Excel в контроллер REST?JAVA

Программисты JAVA общаются здесь
Anonymous
Как динамически загрузить файл Excel в контроллер REST?

Сообщение Anonymous »

Мы переносим наше сеансовое приложение на REST API. В нашем текущем приложении небольшие файлы Excel динамически загружаются путем записи сгенерированной книги Excel в OutputStream, которую мы получаем из объекта HttpResponse. Ниже приведен текущий код для загрузки Excel.

Код: Выделить всё

@RequestMapping(value = "exportRules", method = GET)
public void exportRuleResults(final HttpServletRequest request, final HttpServletResponse response)
{

try {

final SXSSFWorkbook workbook = new SXSSFWorkbook(100);
// summary report
final Sheet sheet = workbook.createSheet("Rules Details");

//Headers (First row of report)
final int rows = writeRulesHeaders(workbook, sheet);
int pageNumber = 0;
final ExportReturnVals ret = new ExportReturnVals(1, true);
ret.rowIndex = rows;
ret.totalnumberOfRecords = 1;

while (ret.hasMoreRows) {
writeRows(pageNumber, sheet, ret, workbook);
pageNumber++;
}
OutputStream os=getOutputStream(response, ret);
workbook.write(os);
response.flushBuffer();
workbook.dispose();
os.close();
} catch (final IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

private OutputStream getOutputStream(final HttpServletResponse response, final   ExcelExportReturnVals ret) throws IOException {

final OutputStream os = response.getOutputStream();
OutputStream outputStream;

if (ret.totalnumberOfRecords > MAX_RECORD_NONZIP) {
response.setContentType("application/zip");
response.addHeader("Content-Disposition", "attachment; filename=\"" + "RuleDetails" + ".zip\"");
outputStream = new ZipOutputStream(new BufferedOutputStream(os));
((ZipOutputStream) outputStream).putNextEntry(new ZipEntry("RuleDetails" + ".xlsx"));
}else {
response.setContentType("application/xlsx");
response.addHeader("Content-Disposition", "attachment; filename=\"" + "RuleDetails" + ".xlsx\"");
outputStream = new BufferedOutputStream(os);
}
return outputStream;
}
Как этого добиться (без сохранения файла по временному пути и динамической загрузки) с помощью REST API, где мы не можем использовать объект HTTP Response?

Подробнее здесь: https://stackoverflow.com/questions/785 ... controller

Вернуться в «JAVA»