通过java实现文件下载
无论在哪台机器上下载都只能下载到项目部署的机器上,而不是本地,如何实现本地下载呢,拜托各位了~~以下是我代码的一部分://单个文件下载publicvoiddownFile...
无论在哪台机器上下载都只能下载到项目部署的机器上,而不是本地,如何实现本地下载呢,拜托各位了~~
以下是我代码的一部分:
// 单个文件下载
public void downFile(String authenticity_token, String url, String fileName) {
PostMethod postMethod = new PostMethod(url);
try {
postMethod.addParameter("authenticity_token", authenticity_token);
postMethod.getParams().setParameter(
HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");// 解决乱码问题,避免传输中文时显示乱码
postMethod.addRequestHeader("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
client.executeMethod(postMethod);
byte[] responseBody = postMethod.getResponseBody();
FileOutputStream fos = new FileOutputStream("d:\\" + fileName);
fos.write(responseBody);
fos.close();
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
postMethod.releaseConnection();
}
} 展开
以下是我代码的一部分:
// 单个文件下载
public void downFile(String authenticity_token, String url, String fileName) {
PostMethod postMethod = new PostMethod(url);
try {
postMethod.addParameter("authenticity_token", authenticity_token);
postMethod.getParams().setParameter(
HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");// 解决乱码问题,避免传输中文时显示乱码
postMethod.addRequestHeader("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
client.executeMethod(postMethod);
byte[] responseBody = postMethod.getResponseBody();
FileOutputStream fos = new FileOutputStream("d:\\" + fileName);
fos.write(responseBody);
fos.close();
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
postMethod.releaseConnection();
}
} 展开
1个回答
展开全部
在jsp/servlet中断点/多线程下载文件
<%@ page import="java.io.File" %><%@ page import="java.io.IOException" %><%@ page import="java.io.OutputStream" %><%@ page import="java.io.RandomAccessFile" %><%! public void downloadFile(HttpServletRequest request, HttpServletResponse response, File file) throws IOException { RandomAccessFile raf = new RandomAccessFile(file, "r"); java.io.FileInputStream fis = new java.io.FileInputStream(raf.getFD()); response.setHeader("Server", "www.trydone.com"); response.setHeader("Accept-Ranges", "bytes"); long pos = 0; long len; len = raf.length(); if (request.getHeader("Range") != null) { response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT); pos = Long.parseLong(request.getHeader("Range") .replaceAll("bytes=", "") .replaceAll("-", "") ); } response.setHeader("Content-Length", Long.toString(len - pos)); if (pos != 0) { response.setHeader("Content-Range", new StringBuffer() .append("bytes ") .append(pos) .append("-") .append(Long.toString(len - 1)) .append("/") .append(len) .toString() ); } response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", new StringBuffer() .append("attachment;filename=\"") .append(file.getName()) .append("\"").toString()); raf.seek(pos); byte[] b = new byte[2048]; int i; OutputStream outs = response.getOutputStream(); while ((i = raf.read(b)) != -1) { outs.write(b, 0, i); } raf.close(); fis.close(); }%><% String filePath = request.getParameter("file"); filePath = application.getRealPath(filePath); File file = new File(filePath); downloadFile(request, response, file);%>
是否可以解决您的问题?
<%@ page import="java.io.File" %><%@ page import="java.io.IOException" %><%@ page import="java.io.OutputStream" %><%@ page import="java.io.RandomAccessFile" %><%! public void downloadFile(HttpServletRequest request, HttpServletResponse response, File file) throws IOException { RandomAccessFile raf = new RandomAccessFile(file, "r"); java.io.FileInputStream fis = new java.io.FileInputStream(raf.getFD()); response.setHeader("Server", "www.trydone.com"); response.setHeader("Accept-Ranges", "bytes"); long pos = 0; long len; len = raf.length(); if (request.getHeader("Range") != null) { response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT); pos = Long.parseLong(request.getHeader("Range") .replaceAll("bytes=", "") .replaceAll("-", "") ); } response.setHeader("Content-Length", Long.toString(len - pos)); if (pos != 0) { response.setHeader("Content-Range", new StringBuffer() .append("bytes ") .append(pos) .append("-") .append(Long.toString(len - 1)) .append("/") .append(len) .toString() ); } response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", new StringBuffer() .append("attachment;filename=\"") .append(file.getName()) .append("\"").toString()); raf.seek(pos); byte[] b = new byte[2048]; int i; OutputStream outs = response.getOutputStream(); while ((i = raf.read(b)) != -1) { outs.write(b, 0, i); } raf.close(); fis.close(); }%><% String filePath = request.getParameter("file"); filePath = application.getRealPath(filePath); File file = new File(filePath); downloadFile(request, response, file);%>
是否可以解决您的问题?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询