How to Download SQL Server File using Servlet

Asked

Viewed 71 times

0

I have a basic application where I save files in the database SQL Server using Java, the part of insert into the bank I’ve done, I’d like to know how to download the file that is in the bank via JSP.

As it is stored in the bank:

inserir a descrição da imagem aqui

My Privacy to download:

package servlet;

import dao.ConnectionFactory;
import dao.FactoryDAO;
import interfaceDAO.IFileDAO;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.FileMODEL;

/**
 *
 * @author Victor
 */
@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);
        }
    }

1 answer

0


Replace Fileoutputstream with Outputstream and enter the file Mime in the header. 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();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.