Salve, sto facendo un progetto didattico e devo inserire in un database una immagine tramite un form html di upload. Successivamente devo poter recuperare l'immagine per visualizzarla in una pagina web.
Il salvataggio dell'immagine nel database sembrerebbe funzionare, mentre il recupero no.
Vi mostro quanto fatto fin'ora.
Ho scritto la seguente servlet che con la chiamata post salva nel DB l'immagine(accedendovi tramite
un nome del prodotto e il nome del fornitore del prodotto, mentre tramite il metodo doGet restituisce
l'immagine:
codice:
public class ManageImagesServlet extends AbstractDatabaseServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String nomeProdotto = (String) request.getParameter("nome-prodotto");
String nomeFornitore = (String) request.getParameter("nome-fornitore");
if (nomeProdotto != null && nomeFornitore != null) {
PrintWriter out = response.getWriter();
int i;
Connection con;
try {
con = DS.getConnection();
SaveTakeImagesDatabase dbms = new SaveTakeImagesDatabase(con);
InputStream is = dbms
.searchPicture(nomeProdotto, nomeFornitore);
BufferedInputStream bis = new BufferedInputStream(is);
response.setContentType("image/jpeg");
response.setContentLength(bis.available());
while ((i = bis.read()) != -1)
out.write(i);
// chiudo lo stream in lettura
bis.close();
} catch (SQLException e) {
// TODO ...
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
MultipartRequest multi = new MultipartRequest(request, "/tmp/");
// dati del form
String nomeProdotto = (String) multi.getParameter("nome-prodotto");
String nomeFornitore = (String) multi.getParameter("nome-fornitore");
File f = multi.getFile("image");
if (f == null) {
// TODO: comunicarlo alla JSP
} else {
// memorizzo nel DB l'immagine
Connection con;
try {
con = DS.getConnection();
SaveTakeImagesDatabase dbms = new SaveTakeImagesDatabase(con);
dbms.storePicture(nomeProdotto, nomeFornitore, f);
//TODO modificare il redirect
response.sendRedirect("../html/image-test.html");
} catch (SQLException e) {
// TODO ..
}
}
}
}
La classe SaveTakeImagesDatabase realizza i metodi per accedere al database:
codice:
public class SaveTakeImagesDatabase {
private final Connection con;
public SaveTakeImagesDatabase(Connection con) {
this.con = con;
}
public void storePicture(String nameP, String nameF, File f)
throws SQLException {
String insertpic = "UPDATE PPJ.Prodotto " + "SET immagine = ? "
+ "WHERE nome =? AND nomeFornitore =?";
PreparedStatement pst = null;
pst = con.prepareStatement(insertpic);
pst.clearParameters();
try {
pst.setBinaryStream(1, new FileInputStream(f), (int) f.length());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pst.setString(2, nameP);
pst.setString(3, nameF);
pst.execute();
con.close();
}
public InputStream searchPicture(String nomeP, String nomeF)
throws SQLException {
String getpic = "SELECT immagine " + "FROM PPJ.Prodotto "
+ "WHERE nome=? AND nomeFornitore=?";
PreparedStatement pstmt = null;
ResultSet rs = null;
InputStream is = null;
pstmt = con.prepareStatement(getpic);
pstmt.clearParameters();
pstmt.setString(1, nomeP);
pstmt.setString(2, nomeF);
rs = pstmt.executeQuery();
rs.next();
is = rs.getBinaryStream("picture");
con.close();
return is;
}
}
Il form per l'upload prende i dati e li comunica alla servlet tramite POST (../images rappresenta la servlet)
codice:
<FORM METHOD="POST" ACTION="../images" ENCTYPE="multipart/form-data">
<INPUT TYPE="TEXT" NAME="nome-prodotto">
<INPUT TYPE="TEXT" NAME="nome-fornitore">
<INPUT TYPE="FILE" NAME="image">
<INPUT TYPE="SUBMIT" NAME="submit" VALUE="upload">
</FORM>
mentre per visualizzare l'mmagine sul browser utilizzo (nome del prodotto: "iPhone 5 64GB", nome fornitore: "Apple" per esempio)
codice:
[img]../images?nome-prodotto=iPhone+5+64GB&nome-fornitore=Apple[/img]
Grazie in anticipo