📄 filedownload.java
字号:
package com.comingnet.tool;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.print.URIException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.db.ProxoolAction;
public class filedownload extends HttpServlet {
/**
* Constructor of the object.
*/
public filedownload() {
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
}
/**
* 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
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String zyid=request.getParameter("zyid");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
String zyym="";
String zybm="";
String zylj="";
try {
conn=ProxoolAction.getConnection();
stmt=conn.prepareStatement("update m_resource set xzcs=xzcs+1 where zyid="+zyid);
stmt.executeUpdate();
ProxoolAction.closeStatement(stmt);
stmt=conn.prepareStatement("select zyym,zybm,zylj from m_resource where zyid="+zyid);
rs=stmt.executeQuery();
while(rs.next())
{
zyym=rs.getString(1);
zybm=rs.getString(2);
zylj=rs.getString(3);
}
} catch (Exception e) {
System.out.println(e.toString());
}finally{
ProxoolAction.closeResultSet(rs);
ProxoolAction.closeStatement(stmt);
}
zyym=URLEncoder.encode(zyym, "utf-8");
ServletContext sc=this.getServletContext();
String abspath=sc.getRealPath("");
abspath=abspath+"\\UploadFiles\\"+zybm;
System.out.println(abspath+"\\UploadFiles\\"+zybm);
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition", "attachment;filename="
+ zyym);
OutputStream os=response.getOutputStream();
FileInputStream fis=null;
try {
fis=new FileInputStream(zylj+"\\"+zybm);
int b=0;
while((b=fis.read())!=-1)
{
os.write(b);
}
} catch (Exception e) {
System.out.println(e.toString());
}finally{
fis.close();
os.flush();
os.close();
}
}
/**
* 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)
throws ServletException, IOException {
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -