Salve a tutti,
ho una servlet che servirebbe a leggere da un campo blob di un db oracle (su cui precedentemente viene salvato un file). Da questo blob viene estratto un InputStream e poi viene scritto sull'OutputStream della response http in modo da permettere al browser di scaricare il file.
Però, non capisco perché il file che viene generato è vuoto.
Un esempio di blob memorizzato a db è il seguente:
2d2d2d205363726970742070657220637265617a696f6e6520 746162656c6c6120666973696361202d2d2d0d0a4352454154 45205441424c4520746573745f626c6f625f66696c650d0a28 0d0a0975706c6f61645f69640909494e54454745520909094e 4f54204e554c4c2c0d0a202020200966696c655f6e616d6509 0956415243484152322831323820425954452920202020094e 4f54204e554c4c2c0d0a20200966696c655f64617461090942 4c4f42202020202020202020202020202020094e4f54204e55 4c4c2c0d0a0d0a09434f4e53545241494e5420506b5f546573 745f426c6f625f46696c65205052494d415259204b45592028 75706c6f61645f6964290d0a29
Questo è il codice della servlet:
codice:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//get upload id from URL's parameters
int uploadId = Integer.parseInt(request.getParameter("id"));
Connection conn = null; // connection to the database
try {
//connects to the database
conn = OracleJDBCConn.creaConnessione();
//queries the database
String sql = "SELECT * FROM test_blob_file WHERE upload_id = ?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setInt(1, uploadId);
ResultSet result = statement.executeQuery();
if (result.next()) {
// gets file name and file blob data
String fileName = result.getString("file_name");
Blob blob = result.getBlob("file_data");
InputStream inputStream = blob.getBinaryStream();
int fileLength = inputStream.available();
System.out.println("fileLength = " + fileLength);
ServletContext context = getServletContext();
// sets MIME type for the file download
String mimeType = context.getMimeType(fileName + ".txt");
if (mimeType == null) {
mimeType = "application/octet-stream";
}
// set content properties and header attributes for the response
response.setContentType(mimeType);
response.setContentLength(fileLength);
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"", fileName + ".txt");
response.setHeader(headerKey, headerValue);
// writes the file to the client
OutputStream outStream = response.getOutputStream();
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = -1;
int offset = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outStream.write(buffer, offset, bytesRead);
//offset += BUFFER_SIZE;
}
inputStream.close();
outStream.close();
} else {
// no file found
response.getWriter().print("File not found for the id: " + uploadId);
}
} catch (SQLException ex) {
ex.printStackTrace();
response.getWriter().print("SQL Error: " + ex.getMessage());
} catch (IOException ex) {
ex.printStackTrace();
response.getWriter().print("IO Error: " + ex.getMessage());
} finally {
if (conn != null) {
// closes the database connection
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
Qualche suggerimento ?
Grazie dell'attenzione