Java下载服务器资源

下载1:

下载1(下载压缩包) 

下载2:

下载2(在线浏览图)

下载3:

jsp:
下载3(下载中文命名资源)
servlet: String resname = req.getParameter("res"); resname = new String(resname.getBytes("ISO-8859-1"),"UTF-8"); String path = servlet.getServletContext().getRealPath("\\download\\"+resname); //设置响应头,以附件方式下载 resp.setHeader("Content-disposition", "attachment;fileName="+URLEncoder.encode(resname, "utf-8")); BufferedInputStream reader = new BufferedInputStream(new FileInputStream(path)); ServletOutputStream writer = resp.getOutputStream(); byte[] bs = new byte[1024]; int len = 0; while((len = reader.read(bs))!=-1){ writer.write(bs, 0, len); } reader.close(); writer.close();

总结

以上为个人笔记,仅供参考!

你可能感兴趣的:(java)