public class FilesDownload extends HttpServlet {
private static final long serialVersionUID = -6849794470754667710L;
private static Log log = LogFactory.getLog(FilesDownload.class);
/**
* Constructor of the object.
*/
public FilesDownload() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
@SuppressWarnings({ "deprecation", "unchecked" })
public void doGet(HttpServletRequest request, HttpServletResponse response) {
try {
String title;
title = request.getParameter("subject");
String method = request.getParameter("method");
if ("download".equals(method)) {
download(request, response);
} else if ("downloadall".equals(method)) {
downloadAll(request, response, title);
}
} catch (SQLException e) {
log.error("");
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) {
response.setContentType("text/html");
PrintWriter out = null;
try {
out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
} catch (Exception e) {
// TODO Auto-generated catch block
log.error(e);
} finally {
if (out != null)
out.close();
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() {
// Put your code here
}
// private void zipFile(FileElement element, OutputStream fw,
// ZipOutputStream zos) {
// if (element.getFileID() != null) {
//
// // zos.setEncoding("GBK");
// FileInputStream fis = null;
// try {
// zos.putNextEntry(new ZipEntry(element.getFileName()));
// fis = (FileInputStream) element.getInputStreamDirect();
// byte[] buffer = new byte[1024];
// int r = 0;
// while ((r = fis.read(buffer)) != -1) {
// zos.write(buffer, 0, r);
// }
// } catch (IOException e) {
// // TODO Auto-generated catch block
// log.error(e);
// } finally {
// try {
// if (fis != null)
// fis.close();
// if (zos != null)
// zos.closeEntry();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// log.error(e);
// }
// }
// }
// }
private void download(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse) throws SQLException {
DataSource dataSource = DataSourceFactory.defaultFactory.getDataSource("dataSource");
Connection conn = null;
// Statement stmt=null;
PreparedStatement ps = null;
ResultSet rs = null;
String down_id = "";
String down_id_string = paramHttpServletRequest.getParameter("uuid");
if (down_id_string == null) {
throw new SystemException("no file exists");
} else {
down_id = down_id_string; // 获得下载的文件id
}
String sql = "select FILE_NAME,FILE_CONTENT from DM_EDOC_BASE_FILE where id= ?";
try {
conn = dataSource.getConnection();
conn.setAutoCommit(false);
ps = conn.prepareStatement(sql);
ps.setString(1, down_id);
// stmt = conn.createStatement();
// rs = stmt.executeQuery(sql);
rs = ps.executeQuery();
if (rs != null) {
if (rs.next()) {
paramHttpServletResponse.setHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode(rs.getString("file_name"), "UTF-8"));
paramHttpServletResponse.setHeader("Connection", "close");
paramHttpServletResponse.setHeader("Content-Type", "application/octet-stream");
ServletOutputStream sout = paramHttpServletResponse.getOutputStream();
InputStream in = rs.getBinaryStream("file_content");
byte b[] = new byte[1024 * 8];
for (int i = in.read(b); i != -1;) {
sout.write(b);
i = in.read(b);
}
sout.flush();
sout.close();
in.close();
}
}
} catch (SQLException e) {
log.error("执行sql报错:" + e);
throw new SystemException("执行查询报错");
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.debug(e);
}
} finally {
try {
// if (rs.isClosed()) {
rs.close();
// }
} catch (SQLException e) {
if (log.isErrorEnabled()) {
log.error(EdocDownload.class + "关闭结果集时报错", e);
}
}
try {
if (null != ps) {
ps.close();
}
} catch (SQLException e) {
if (log.isErrorEnabled()) {
log.error(EdocDownload.class + "关闭SQL执行器时报错", e);
}
}
try {
if (null != conn) {
conn.close();
}
} catch (SQLException e) {
if (log.isErrorEnabled()) {
log.error(EdocDownload.class + "关闭连接对象时报错", e);
}
}
}
}
private void downloadAll(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String title) {
String str1 = paramHttpServletRequest.getParameter("uuids");
if (str1 == null)
throw new SystemException("uuids error");
ServletOutputStream localServletOutputStream = null;
try {
localServletOutputStream = paramHttpServletResponse.getOutputStream();
List localList = StringUtil.split(str1, ",");
if (localList.size() == 0)
localServletOutputStream.close();
String str2 = "attachment;filename=\"" + URLEncoder.encode(title, "UTF-8") + ".zip\"";
paramHttpServletResponse.setContentType("APPLICATION/OCTET-STREAM;charset=UTF-8");
paramHttpServletResponse.setHeader("Content-disposition", str2);
FileFactory.getCompressedArchive(localList, localServletOutputStream);
} catch (IOException localIOException1) {
log.error(localIOException1);
try {
if (localServletOutputStream != null)
localServletOutputStream.close();
} catch (IOException localIOException2) {
log.error(localIOException2);
}
} catch (CMException localCMException) {
log.error(localCMException);
try {
if (localServletOutputStream != null)
localServletOutputStream.close();
} catch (IOException localIOException3) {
log.error(localIOException3);
}
} finally {
try {
if (localServletOutputStream != null)
localServletOutputStream.close();
} catch (IOException localIOException4) {
log.error(localIOException4);
}
}
}
}
12-04
03-26
12-11