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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Get ID from request.
String fileId = request.getParameter("id");
//Parse it to int ...THINK about hash() for security ...
int id =Integer.parseInt(fileId);
// Check if ID is supplied to the request.
if (fileId == null||Integer.parseInt(fileId)==0) {
// Do your thing if the ID is not supplied to the request.
// Throw an exception, or send 404, or show default/warning page, or just ignore it.
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}
// Lookup for file(blob) by FileId in database.
try{
setReference( ReferenceBean().findReference(id));
}catch (Exception e)
{e.printStackTrace();}
// Check if file is actually retrieved from database.
if (getReference() == null) {
// Do your thing if the file does not exist in database.
// Throw an exception, or send 404, or show default/warning page, or just ignore it.
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}
// Init servlet response.
response.reset();
response.setBufferSize(DEFAULT_BUFFER_SIZE);
//response.setHeader("Content-Type", file.getContentType());
response.setHeader("Content-Type",client.getLogotype());
response.setHeader("Content-Length", String.valueOf(client.getLogo().length));
response.setHeader("Content-Disposition", "inline; filename=\"" + client.getNom() + "\"");
// Prepare streams.
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
// Open streams.
SerialBlob media = new SerialBlob(client.getLogo());
input = new BufferedInputStream(media.getBinaryStream(), DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
System.out.println("Lenght Media Serial:"+media.length()+"");
// Write file contents to response.
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
// Finalize task.
output.flush();
}
catch (Exception e){e.printStackTrace();}
finally {
// Gently close streams.
close(output);
close(input);
}
} |
Partager