1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
long docId=Long.parseLong( request.getParameter("docId") );
DocumentHelper docHelper = DocumentHelper.getInstance(); MappedRecord pj = docHelper.recupererPJ(docId);
InputStream inputStream = (InputStream)pj.get("stream");
String fileName = (String) pj.get("file_name");
String mimeType = (String) pj.get("mime_type");
//récupération de la taille du fichier
int availableBytes=0;
try {
availableBytes = inputStream.available();
} catch (IOException e) {
e.printStackTrace();
}
byte [] data = new byte[availableBytes];
response.setContentType(mimeType);
response.setContentLength(availableBytes);
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName.trim().substring(1,fileName.length()) + "\";");
OutputStream os = response.getOutputStream();
int count;
while( (count = inputStream.read(data)) > -1 ) {
os.write(data, 0, count);
}
os.flush();
os.close();
return null;
} |