开发者

Downloading attachment from database using java [duplicate]

This question already has an answer here: Submitting form to Servlet which interacts with database results in blank page (1 answer) Closed 2 years ago.

I have used the following code for downloading the attachment from database but the downloaded file is of 0 bytes. What is the cause and how can I fix it? Here is the code:

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author ROHIT
 */
@WebServlet(name = "DownloadAttachment", urlPatterns = {"/DownloadAttachment"})
public class DownloadAttachment extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


        String mailID = request.getParameter("username");
        String fileName = request.getParameter("filename");

        response.setContentType("application/msword");
        response.setHeader("Content-Disposition", "attachment; filename=" + "\"" + fileName + "\"");

        OutputStream os = null;
        try {
            os = response.getOutputStream();
        } catch (IOException ioe0) {
            ioe0.printStackTrace();
        }


        String driverclass="oracle.jdbc.driver.OracleDriver";
        String dburl="jdbc:oracle:thin:@ROHIT-PC:1521:XE";
        String dbuser="system";
        String dbpassword="jaihanuman";

        PreparedStatement ptmt = null;
        Connection con = null;
        ResultSet rs;


        try {

                Class.forName(driverclass);
            con=DriverManager.getConnection(dburl,dbuser,dbpassword);
                String sql = "select filedata from attachfile where filename = ? where mailid = ?";
                ptmt=con.prepareStatement(sql);
                ptmt.setString(1, fileName);
                ptmt.setString(2, mailID);
                rs = ptmt.executeQuery();


            while (rs.next()) {
                BufferedInputStream pdfData = new BufferedInputStream(rs.getBinaryStream("filedata"));
                byte[] buf = new byte[100 * 1024];
                int len;
                while ((len = pdfData.read(buf, 0, buf.length)) != -1) {
                    os.write(buf, 0, len);
                }
            }
            rs.close();
            con.close();

        } catch (Exception ee) {
            System.out.println("Failure in " + ee.toString());
            ee.printStackTrace();
        }

    }

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

/** 
 * Handles the HTTP <code>GET</code> method.
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
    protected

void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);


} 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet resp开发者_StackOverflow中文版onse
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */

        @Override
        protected void

doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);


}

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */

        @Override
        public String

getServletInfo() {
        return "Short description";

}// </editor-fold>
}


Your SQL statement is wrong: you have used

"select filedata from attachfile where filename = ? where mailid = ?"

when you mean to use

"select filedata from attachfile where filename = ? and mailid = ?"


I think you need close the outputstream ...

Example: rs.close(); con.close(); os.flush(); os.close();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜