日期:2014-05-17 浏览次数:20857 次
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
int wlen = 0; while (wlen < len) { int read = bis.read(b, 0, len); wlen += read; oSavedFile.write(b, 0, read); }