download.java~1~
来自「JSP企业信息交流系统程序CICS v1.0: 功能描述: 1:管理」· JAVA~1~ 代码 · 共 71 行
JAVA~1~
71 行
package inc;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Download extends HttpServlet {
public void init() throws ServletException {
}
public void destroy() {
super.destroy();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String root = getServletContext().getRealPath("/");
String path="uploadFiles\\";
String name = request.getParameter("name");
//PrintWriter out=response.getWriter();
HttpSession session = request.getSession();
String username = (String) session.getAttribute("username");
String id = (String) session.getAttribute("id");
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition","attachment; filename=\"" + name + "\"");
response.setContentLength((int)name.length());
if (username == null) {
response.sendRedirect("index.html");
return;
}
try
{
byte[] b = new byte[1024];
int i = 0;
String downpath=root + path + name;
FileInputStream fis = new FileInputStream(downpath);
ServletOutputStream os = response.getOutputStream();
while ( (i = fis.read(b)) != -1 )
{
os.write(b, 0, i);
}
fis.close();
os.flush();
os.close();
}
catch ( Exception e )
{
e.printStackTrace();
}
}
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException {
doGet(req, resp);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?