I have a basic application where I save files in the database SQL Server
using Java
, the insert part in the database I already did, I would like to know how do I
to download the file that is in the bank via JSP
.
As stored in the database:
Myservlettodownload:
packageservlet;importdao.ConnectionFactory;importdao.FactoryDAO;importinterfaceDAO.IFileDAO;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.sql.Connection;importjava.sql.SQLException;importjava.util.logging.Level;importjava.util.logging.Logger;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importmodel.FileMODEL;/****@authorVictor*/@WebServlet(name="Download", urlPatterns = {"/Download"})
public class Download extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
Connection conn;
IFileDAO dao = FactoryDAO.create_FileDAO();
int id;
FileMODEL attachment = null;
conn = ConnectionFactory.getConnection();
id = Integer.parseInt(request.getParameter("id"));
attachment = dao.searchById(id);
try {
byte[] b = attachment.getFILE_CONTENT();
FileOutputStream fileOutputStream = new FileOutputStream(new File("Downloads/aluno.xml"));
fileOutputStream.write(b);
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(Download.class
.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(Download.class
.getName()).log(Level.SEVERE, null, ex);
}
}