Java 未找到MIME媒体类型应用程序/vnd.ms-excel
我正在尝试生成一个excel文件,其中包含有关度量的数据。此文件将在Rest服务上返回Java 未找到MIME媒体类型应用程序/vnd.ms-excel,java,rest,jersey,bytearrayoutputstream,media-type,Java,Rest,Jersey,Bytearrayoutputstream,Media Type,我正在尝试生成一个excel文件,其中包含有关度量的数据。此文件将在Rest服务上返回 @GET @Path("{sensorId}/csv") @Produces("text/csv") public Response getVariablePointsInRangeAsCSV(@PathParam("sensorId") String sensorId,
@GET
@Path("{sensorId}/csv")
@Produces("text/csv")
public Response getVariablePointsInRangeAsCSV(@PathParam("sensorId") String sensorId,
@QueryParam("filter") FlowCause filter,
@QueryParam("from") String fromParam,
@QueryParam("until") String untilParam) throws Throwable {
List<Alivio> alivioList;
if (fromParam.isEmpty() || untilParam.isEmpty())
alivioList = aliviosService.getAliviosForSensor(sensorId, filter, null, null);
else
alivioList = aliviosService.getAliviosForSensor(sensorId, filter, PARAMS_DATE_FORMAT.parse(fromParam), PARAMS_DATE_FORMAT.parse(untilParam));
ByteArrayOutputStream baos = getExcelByteArrayOutputStreamForMeasures(sensorId,
CONSUMPTION_VARIABLE_NAME, alivioList);
return Response
.ok(baos.toByteArray())
.type("application/vnd.ms-excel")
.header("Content-disposition",
"attachment; filename=\"" + sensorId + "-" + CONSUMPTION_VARIABLE_NAME + ".xls\"")
.build();
}
private ByteArrayOutputStream getExcelByteArrayOutputStreamForMeasures(String sensorId,
String variableName, List<Alivio> listAlivios) throws IOException,
WriteException {
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("es", "ES"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
WritableWorkbook workbook = Workbook.createWorkbook(baos, wbSettings);
workbook.createSheet(sensorId + "-" + variableName, 0);
WritableSheet excelSheet = workbook.getSheet(0);
WritableCellFormat cellFormat = new WritableCellFormat(new WritableFont(WritableFont.ARIAL,
10));
int index = 0;
for (Alivio alivio : listAlivios) {
excelSheet.addCell(new Label(0, index, EXCEL_TIMESTAMP_FORMAT.format(alivio.getStart())
, cellFormat));
excelSheet.addCell(new Label(1, index, EXCEL_TIMESTAMP_FORMAT.format(alivio.getEnd())
, cellFormat));
excelSheet.addCell(new Label(2, index, Double.toString(alivio.getVolume())
, cellFormat));
excelSheet.addCell(new Label(3, index, alivio.getCause().toString()
, cellFormat));
index++;
}
for (int x = 0; x < 4; x++) {
CellView columnView = excelSheet.getColumnView(x);
columnView.setAutosize(true);
excelSheet.setColumnView(x, columnView);
}
workbook.write();
workbook.close();
return baos;
}
谢谢大家! 我解决了向POM添加
jersey bundle
依赖项的问题
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.19</version>
</dependency>
泽西岛
球衣束
1.19
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.19</version>
</dependency>