【发布时间】:2016-05-10 21:15:17
【问题描述】:
我想从互联网上下载一个文件,我有那个文件的 url。所以我写了一个下载servlet:
public class DownloadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
String pathToDownload = request.getParameter("url");
URL url = new URL(pathToDownload);
URLConnection uc = url.openConnection();
String contentType = uc.getContentType();
int contentLength = uc.getContentLength();
InputStream is = uc.getInputStream();
response.setContentType(contentType);
// resp.setHeader("Content-Disposition", "attachment; filename*=\"utf-8''" + filename + "");
ServletOutputStream os = response.getOutputStream();
byte[] b = new byte[2048];
int length;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
os.close();
}
}
我想在用户点击文件时显示弹出窗口是否保存,所以有
resp.setHeader("Content-Disposition", "attachment; filename*=\"utf-8''" + filename + "");
但我希望文件名与内部文件名相同,所以上面的 sn-p 还需要什么?
【问题讨论】: