81,094
社区成员
发帖
与我相关
我的任务
分享
public String upload(URL url) throws IOException{
String fileName = url.getFile();
InputStream in = null;
final long size = 1024*5;
if(fileName.equals("")){
throw new IOException("upload webfile \"" + fileName + "\" failed");
}
HttpURLConnection con = (HttpURLConnection) url.openConnection();
long total = con.getContentLength();
System.out.println("file size is "+total);
String newFileName = null;
con.disconnect();
if(total<=size){
con = (HttpURLConnection) url.openConnection();
con.connect();
in = con.getInputStream();
return upload(fileName, in);
}else {
long part = total%size != 0 ? total/size+1 : total/size;
long len;
byte[] b = null;
for(int i = 0; i< part;i++){
len = i!=part-1 ? size : total%size;
b = new byte[IoUtils.safeLongToInt(len)];
//con.setConnectTimeout(30000);
con.setReadTimeout(30000);
//设置传输位置
con.setRequestProperty("RANGE","bytes=" + i*size + "-" + (i*size+len-1) + "/" + total);
//设置请求信息
con.setRequestProperty("GET","/large/774fca33gw1dozmkrsu6ej.jpg HTTP/1.1");
//设置接受信息
con.setRequestProperty("[code=Java]Accept
","image/gif,image/x-xbitmap,application/msword,*/*");