protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
// 获取下载文件路径
// 获取应用
ServletContext ctx = request.getServletContext();// 首先获取上下文对象
String basePath = ctx.getRealPath("/WEB-INF/data/");// 获取需要配置文件的下载位置,默认相对路径
File file=new File("D:/12/");//自定义路径
basePath=file.getAbsolutePath();
System.out.println(basePath);
// 获取用户要求的下载文件名字
String fileName = request.getParameter("filename");
//fileName = fileName == null ? "123.jpg":fileName;
fileName="123.jpg";
// 根据 文件存放路径 及用户要求路径 生成 完整路径
Path fullPath = Paths.get(basePath + "/" + fileName);//路径资源
System.out.print(fullPath);
System.out.println("11111111111111111");
if(Files.exists(fullPath)) {
String encodedFileName = URLEncoder.encode(fileName,"UTF-8");// 对中文文件名进行 否则会出现中文无法正常使用的问题
// 设置返回头信息
response.setHeader("content-disposition", "attachment; filename="+encodedFileName);// 设置响应消息体的处理方式(内联 include 附件 attachment)
response.setHeader("content-disposition", "include; filename="+encodedFileName);// 设置响应消息体的处理方式(内联 include 附件 attachment)
Files.copy(fullPath, response.getOutputStream());// 复制文件到输出流
}
// 类结束了
}
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
// 获取下载文件路径
// 获取应用
ServletContext ctx = request.getServletContext();// 首先获取上下文对象
String basePath = ctx.getRealPath("/WEB-INF/data/");// 获取需要配置文件的下载位置,默认相对路径
File file=new File("D:/12/");//自定义路径
basePath=file.getAbsolutePath();
System.out.println(basePath);
// 获取用户要求的下载文件名字
String fileName = request.getParameter("filename");
//fileName = fileName == null ? "123.jpg":fileName;
fileName="123.jpg";
// 根据 文件存放路径 及用户要求路径 生成 完整路径
Path fullPath = Paths.get(basePath + "/" + fileName);//路径资源
System.out.print(fullPath);
System.out.println("11111111111111111");
if(Files.exists(fullPath)) {
String encodedFileName = URLEncoder.encode(fileName,"UTF-8");// 对中文文件名进行 否则会出现中文无法正常使用的问题
// 设置返回头信息
response.setHeader("content-disposition", "attachment; filename="+encodedFileName);// 设置响应消息体的处理方式(内联 include 附件 attachment)
response.setHeader("content-disposition", "include; filename="+encodedFileName);// 设置响应消息体的处理方式(内联 include 附件 attachment)
Files.copy(fullPath, response.getOutputStream());// 复制文件到输出流
}
// 类结束了
}