How to download SQL Server file using Servlet

0

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);
        }
    }
    
asked by anonymous 18.12.2018 / 20:57

1 answer

0

Replace FileOutputStream with OutputStream and indicate in the header the file's Mime. Follow below:

 try {
    response.setContentType("application/xml");
    response.setHeader("Content-Disposition", "attachment;filename=aluno.xml");

    byte[] strBytes = attachment.getFILE_CONTENT();
    response.setContentLength(strBytes.length);
    OutputStream outStream = response.getOutputStream();
    outStream.write( strBytes );
    outStream.flush();
    outStream.close();
} catch (Exception e) {
    e.printStackTrace();
} finally {
    conn.close();
}
    
19.12.2018 / 18:30