Ciao, volevo restituire un file da un server resteasy. A tale scopo, ho un collegamento sul lato client che sta chiamando un servizio di assistenza con ajax. Voglio restituire il file nel servizio di riposo. Ho provato questi due blocchi di codice, ma entrambi non funzionavano come volevo.Restituisci file dal server Resteasy
@POST
@Path("/exportContacts")
public Response exportContacts(@Context HttpServletRequest request, @QueryParam("alt") String alt) throws IOException {
String sb = "Sedat BaSAR";
byte[] outputByte = sb.getBytes();
return Response
.ok(outputByte, MediaType.APPLICATION_OCTET_STREAM)
.header("content-disposition","attachment; filename = temp.csv")
.build();
}
.
@POST
@Path("/exportContacts")
public Response exportContacts(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam("alt") String alt) throws IOException {
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=temp.csv");
ServletOutputStream out = response.getOutputStream();
try {
StringBuilder sb = new StringBuilder("Sedat BaSAR");
InputStream in =
new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
byte[] outputByte = sb.getBytes();
//copy binary contect to output stream
while (in.read(outputByte, 0, 4096) != -1) {
out.write(outputByte, 0, 4096);
}
in.close();
out.flush();
out.close();
} catch (Exception e) {
}
return null;
}
Quando ho controllato dalla console di Firebug, entrambi questi blocchi di codice scritto "Sedat Basar" in risposta alla chiamata AJAX. Tuttavia, voglio restituire "Sedat BaSAR" come file. Come lo posso fare?
Grazie in anticipo.
Forse si finisce per trovare una soluzione a questo? – rabs