参考其他人博客总结:
客户端:
/**
* 下载
* @param locationFilePath 本地已下载的文件路径
* @param url 请求服务端路径
*/
public static void download(String locationFilePath,String url) {
FileOutputStream fos =null;
try {
File file = new File(locationFilePath);
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
//模拟成浏览器
//connection.setRequestProperty("User-Agent", " Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36");
connection.setRequestMethod("GET");
long sum = 0;
if (file.exists()) {
sum = file.length();
connection.setRequestProperty("Range", "bytes=" + file.length() + "-");
}
int code = connection.getResponseCode();
System.out.println("code = " + code);
if (code == 200 || code == 206) {
long contentLength = connection.getContentLengthLong();
System.out.println("contentLength = " + contentLength);
contentLength += sum;
InputStream is = connection.getInputStream();
/*
*
* 创建一个向具有指定 name 的文件中写入数据的输出文件流。
* true表示当文件在下载过程中出现中断,
* 当再次链接网络时,将会从断点处追加。
*
* */
fos = new FileOutputStream(file, true);
byte[] buffer = new byte[102400];
int length;
//long startTime = System.currentTimeMillis();
while ((length = is.read(buffer)) != -1) {
fos.write(buffer, 0, length);
sum += length;
/* float percent = sum * 100.0f / contentLength;
System.out.print("\r[");
int p = (int) percent / 2;
for (int i = 0; i < 50; i++) {
if (i < p) {
System.out.print('=');
} else if (i == p){
System.out.print('>');
} else {
System.out.print(' ');
}
}
System.out.print(']');
System.out.printf("\t%.2f%%", percent);
long speed = sum * 1000 / (System.currentTimeMillis() - startTime);
if (speed > (1 << 20)) {
System.out.printf("\t%d MB/s", speed >> 20);
} else if (speed > (1 << 10)) {
System.out.printf("\t%d KB/s", speed >> 10);
} else {
System.out.printf("\t%d B/s", speed);
}*/
}
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(fos!=null){
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
服务端:
/**
* 下载
* @param bN 服务端文件路径
* @param response
* @param request
*/
@RequestMapping(value = "/downloadClient",method = RequestMethod.GET)
public void downloadClient(@RequestParam String bN, HttpServletResponse response,HttpServletRequest request){
try {
//1.获取要下载的文件的绝对路径
String filePath = configUtil.geturl("service.zipUrl") + bN + "\\" + bN + ".zip";
//2.获取要下载的文件
File file1 = new File(filePath);
//3.设置content-disposition响应头控制浏览器以下载的形式打开文件
//response.setHeader("content-disposition", "attachment;filename="+file1.getName());
//设置长度
String header = request.getHeader("Range");
//得到文件块开始的字节
long start =0;
if(header!=null){
start = Long.parseLong((request.getHeader("Range").replaceAll("bytes=", "").split("-")[0]));
}
//设置的返回 文件的大小
response.setContentLengthLong(file1.length()-start); //对应于HttpURLConnection.getContentLengthLong()
//4.获取要下载的文件输入流
InputStream in = new FileInputStream(file1);
in.skip(start);
int len = 0;
//5.创建数据缓冲区
byte[] buffer = new byte[1024];
//6.通过response对象获取OutputStream流
OutputStream out = response.getOutputStream();
//7.将FileInputStream流写入到buffer缓冲区
while ((len = in.read(buffer)) > 0) {
//8.使用OutputStream将缓冲区的数据输出到客户端浏览器
out.write(buffer,0,len);
}
in.close();
}catch (Exception e){
logger.error("downloadClient()",e);
}
}