我正在尝试编写一个功能,我称之为REST端点,这使我得到了content-type: application/pdf
的休息响应,看起来像这样:
%PDF-1.4
%����
4304 0 obj
4330 0 obj
<</C 1636/Filter/FlateDecode/I 1658/Length 1188/S 1431/T 1552>>stream
h�bb��������������cP�a\h�b�q���K��f���X���%�s<�o��
��u.{��b�'�6��&����:�mV�ꛒ��妴�قU�"��I�\T�.��l��
当我试图获得这个身体的类时,它告诉我它是一个字符串。
这就是我的控制器的样子......
def download(){
try{
def documentContent = //rest response//
if(documentContents){
String body = documentContents.body
byte[] data = body.getBytes("UTF-8")
response.setContentType("application/pdf")
response.setContentLength(data.length)
response.setCharacterEncoding("UTF-8")
response.setHeader("Content-disposition","attachment;filename=test.pdf")
response.outputStream << data
response.outputStream.flush()
response.outputStream.close()
return
}
}
catch(Exception e){
println("abc")
}
render status: 204
从我的控制器中,我得到一个包含这些页面的pdf文件,但它都是空白的。
答案 0 :(得分:1)
我现在没有看到任何重大差异,但这对我有用......
def generatePdf(){
ByteArrayOutputStream docStream = /*call that generates pdf data*/
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, 'inline; filename=print.pdf')
response.setContentType('applicaton/pdf')
response.contentLength = docStream.size()
response.outputStream << docStream.toByteArray()
}
明确说明为什么会这样:注意缺少response.setCharacterEncoding()。如果响应是某种明文风格,那么使用response.setCharacterEncoding()是合适的。在您的情况下,响应是PDF,二进制文件,而不是文本。
答案 1 :(得分:0)
这是调整后的代码..但我仍然得到相同的结果。
def generatePDF(){
String body = documentContents.responseEntity.body
ByteArrayOutputStream docStream = new ByteArrayOutputStream()
docStream.write(body.bytes) /* Write bytes into byte array*/
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=test.pdf")
response.setContentType("application/pdf")
response.setCharacterEncoding("UTF-8")
response.contentLength = docStream.size()
response.outputStream << docStream.toByteArray()
response.outputStream.flush()
response.outputStream.close()
}
答案 2 :(得分:0)
您也可以使用
// assuming you have already retrieved the file
// you can also use new File(path) instead of data
render(file: data, fileName: "fileName.pdf", contentType: "application/pdf")
仅供参考:这适用于Grails 3.x.它没有使用旧版本进行测试。
答案 3 :(得分:0)
使用提供PDF文件字节流的REST服务,您可以基于输入流接收数据,因此可以利用强大的Apache IOUtils库。 参见下面的控制器代码(在GRAILS3上测试):
public String get_CellData(int rownum, int colnum) throws Exception {
try {
cell = sheet.getRow(rownum).getCell(colnum);
String CellData = null;
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.println("The type of cell is STRING ");
CellData = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.println("The type of cell is NUMERIC ");
if (DateUtil.isCellDateFormatted(cell)) {
//CellData = cell.getDateCellValue().toString();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
CellData = sdf.format(cell.getDateCellValue());
} else {
String typeCell = cell.getCellStyle().getDataFormatString();
System.out.println("typeCell: " + typeCell);
if (typeCell.contains("%")) {
// DecimalFormat df5 = new DecimalFormat( "#,###,###,##0.00000");
DecimalFormat df5 = new DecimalFormat("#######.##0.00000");
Double value = cell.getNumericCellValue() * 100;
CellData = df5.format(value) + "%";
System.out.println("Percent value found = " + CellData.toString());
} else {
DataFormatter formatter = new DataFormatter();//("#,##0.00");
int formatIndex = cell.getCellStyle().getDataFormat();
String cellFormattedData = formatter.formatRawCellContents(cell.getNumericCellValue(), formatIndex, "#0.00");
//String cellFormattedData = formatter.formatRawCellContents(cell.getNumericCellValue(), formatIndex, "###.##");
CellData = cellFormattedData;
}
}
break;
case Cell.CELL_TYPE_BLANK:
System.out.println("The type of cell is BLANK ");
CellData = "";
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.println("The type of cell is BOOLEAN ");
CellData = Boolean.toString(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
System.out.println("The type of cell is FORMULA ");
System.out.println("Formula is " + cell.getCellFormula());
switch (cell.getCachedFormulaResultType()) {
case Cell.CELL_TYPE_NUMERIC:
System.out.println("Last evaluated as: " + cell.getNumericCellValue());
DataFormatter formatter = new DataFormatter();//("#,##0.00");
int formatIndex = cell.getCellStyle().getDataFormat();
String cellFormattedData = formatter.formatRawCellContents(cell.getNumericCellValue(), formatIndex, "#,##0.00");
CellData = cellFormattedData;
break;
case Cell.CELL_TYPE_STRING:
System.out.println("Last evaluated as \"" + cell.getRichStringCellValue() + "\"");
CellData = cell.getStringCellValue();
break;
}
}
return CellData;
} catch (Exception e) {
return "";
}
}