我使用以下代码从数据库下载附件,但下载的文件为0字节。原因是什么?如何解决?这是代码:
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 response
* @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>
}
答案 0 :(得分:1)
您的SQL语句错误:您已使用
"select filedata from attachfile where filename = ? where mailid = ?"
当你打算使用
时 "select filedata from attachfile where filename = ? and mailid = ?"
答案 1 :(得分:-1)
我认为您需要关闭输出流...
示例:rs.close(); con.close(); os.flush(); os.close();