最简单的下载方式 对某个资源写一个<a href="xxx">下载</a> 但是此方法会被盗链 没人使用
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String name = req.getParameter("name");
//第一步:设置响应的类型
resp.setContentType("application/force-download");
//第二读取文件
String path = getServletContext().getRealPath("/up/"+name);
InputStream in = new FileInputStream(path);
//设置响应头
//对文件名进行url编码
name = URLEncoder.encode(name, "UTF-8");
resp.setHeader("Content-Disposition","attachment;filename="+name);
resp.setContentLength(in.available());
//第三步:开始文件copy
OutputStream out = resp.getOutputStream();
byte[] b = new byte[1024];
int len = 0;
while((len=in.read(b))!=-1){
out.write(b,0,len);
}
out.close();
in.close();
}
URLConnection
此类用于在java代码中模拟浏览器组成http协议向服务发请求(get/post)。
public class OneServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse resp)
throws ServletException, IOException {
String name = request.getParameter("name");
System.err.println("这是get、、、、"+name);
resp.setContentType("text/html;charset=UTF-8");
resp.getWriter().print("你好:"+name);
}
public void doPost(HttpServletRequest request, HttpServletResponse resp)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
System.err.println("这是post请求......."+name);
resp.setContentType("text/html;charset=UTF-8");
resp.getWriter().print("你好:"+name);
}
}
用urlconnection访问oneSerlvet
public class Demo {
/**
* 发送get请求
*
* @throws Exception
*/
@Test
public void testConn() throws Exception {
// 第一步:声明url
String urlPath = "http://localhost:8080/day22_cos/OneServlet?name=Jack";
// 第二步:声明URL对象
URL url = new URL(urlPath);
// 第三步:从url上获取连接
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// 第四步:设置访问的类型
con.setRequestMethod("GET");
// 第五步:设置可以向服务器发信息。也可以从服务器接收信息
con.setDoInput(true);// 设置可以向服务器发信息
con.setDoOutput(true);// 也可以从服务器接收信息
// 第六步:连接
con.connect();
// 7:检查连接状态
int code = con.getResponseCode();
if (code == 200) {
// 8:从服务器读取数据
InputStream in = con.getInputStream();
byte[] b = new byte[1024];
int len = 0;
while ((len = in.read(b)) != -1) {
String s = new String(b, 0, len, "UTF-8");
System.err.print(s);
}
}
// 9:断开
con.disconnect();
}
/**
* 以下发送post请求
*/
@Test
public void post() throws Exception {
// 第一步:声明url
String urlPath = "http://localhost:8080/day22_cos/OneServlet";
// 第二步:声明URL对象
URL url = new URL(urlPath);
// 第三步:从url上获取连接
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// 第四步:设置访问的类型
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
// 第五步:设置可以向服务器发信息。也可以从服务器接收信息
con.setDoInput(true);// 设置可以向服务器发信息
con.setDoOutput(true);// 也可以从服务器接收信息
// 第六步:发信息
// 获取输出流
OutputStream out = con.getOutputStream();
out.write("name=张三".getBytes("UTF-8"));
// 7:检查连接状态
int code = con.getResponseCode();
if (code == 200) {
// 8:从服务器读取数据
InputStream in = con.getInputStream();
byte[] b = new byte[1024];
int len = 0;
while ((len = in.read(b)) != -1) {
String s = new String(b, 0, len, "UTF-8");
System.err.print(s);
}
}
// 9:断开
con.disconnect();
}
}
单线程断点下载
服务使用断点下载时,响应的信息是206。
UrlConnection - HttpurlConnection。-通过URL来获取urlconnection实例。
UrlConnection - HttpurlConnection。-通过URL来获取urlconnection实例。
第一步:正常下载
public class CommonDown {
public static void main(String[] args) throws Exception {
String path = "http://localhost:8080/day22_cos/up/video.avi";
URL url = new URL(path);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setDoInput(true);
con.connect();
int code = con.getResponseCode();
System.err.println(code);
if (code == 200) {
//获取文件大小
long size = con.getContentLength();
System.err.println("总大小是:"+size);
//声明下载到的字节
long sum=0;
BigDecimal bd = new BigDecimal(0D);
double already = 0D;
InputStream in = con.getInputStream();
byte[] b = new byte[1024];
int len = -1;
OutputStream out = new FileOutputStream("f:/video.avi");
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
sum=sum+len;
double percent = ((double)sum)/((double)size);
percent*=100;
bd = new BigDecimal(percent);
bd = bd.divide(new BigDecimal(1),0,BigDecimal.ROUND_HALF_UP);
if(bd.doubleValue()!=already){
System.err.println(bd.intValue()+"%");
already=bd.doubleValue();
}
}
out.close();
}
}
}
第二步:断点下载
1:如何通知服务器只给我3以后数据。
req.setHeader("range","bytes=0-"); 从第0字节以后的所有字节
range=”bytes=3-”
2:我如何知道自己已经下载的3K数据。
读取文件大小。
file.length();
3:如果从当前已经下载的文件后面开始追加数据。
FileRandomAccess 随机访问文件对象
seek(long);
skip(long);
req.setHeader("range","bytes=0-"); 从第0字节以后的所有字节
range=”bytes=3-”
2:我如何知道自己已经下载的3K数据。
读取文件大小。
file.length();
3:如果从当前已经下载的文件后面开始追加数据。
FileRandomAccess 随机访问文件对象
seek(long);
skip(long);
public class BreakDown {
public static void main(String[] args) throws Exception {
String fileName = "video.avi";
String path = "http://localhost:6666/day22_cos/up/"+fileName;
String savePath = "d:/a/"+fileName;
File file = new File(savePath);
long size = file.length();
System.err.println(file.length());
URL url = new URL(path);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
//设置下载区间
con.setRequestProperty("range","bytes="+size+"-");
con.connect();
int code = con.getResponseCode();//只要断点下载,返回的已经不是200,206
System.err.println(code);
if(code==206){
InputStream in= con.getInputStream();
int serverSize = con.getContentLength();
System.err.println("服务器返回的长度:"+serverSize);
System.err.println("这次从哪开开始写:"+size);
//必须要使用
RandomAccessFile out = new RandomAccessFile(file,"rw");
out.seek(size);
byte[] b = new byte[1024];
int len = -1;
while((len=in.read(b))!=-1){
out.write(b,0,len);
}
out.close();
}
}
}