2
回答
java 数据流的问题
注册华为云得mate10,2.9折抢先购!>>>   

我想知道,如何把其它网站上用数据流抛出的文件,用java保存到本地

例如这个:http://app.m.360.cn/wsjbb/download.jsp?t=22927&soft_id=22927

他的地址不是http://xxx.com/sss.jpg

如果是这样的就好了,我可以用:

URL urll = new URL(url);
URLConnection conn = urll.openConnection();
conn.connect();

BufferedReader is = new BufferedReader(new InputStreamReader(conn
.getInputStream(), "utf-8"));

StringBuffer buffer = new StringBuffer();
String str;

while ((str = is.readLine()) != null) {
  buffer.append(str);
  buffer.append("\n");
}
………………………

用数据流抛出的,我搜了一下午,没有头绪……

请大神指教……

举报
玉良
发帖于6年前 2回/351阅
共有2个答案 最后回答: 6年前
 public GetFileOnNet(){

 try {
 byte[] buff = new byte[1024];
 File out = new File("out.apk");
 FileOutputStream fileOutputStream = new FileOutputStream(out);
 
 String str = "http://app.m.360.cn/wsjbb/download.jsp?t=22927&soft_id=22927";
 URL url = new URL(str);
 URLConnection connection = url.openConnection();
 connection.connect();
 
 InputStream is = connection.getInputStream();
 while(is.read(buff)>0){
 fileOutputStream.write(buff);
 }
 fileOutputStream.flush();
 is.close();
 fileOutputStream.close();
 
 
 } catch (Exception e) {
 e.printStackTrace();
 }
 }


引用来自“珠峰”的答案

 public GetFileOnNet(){

 try {
 byte[] buff = new byte[1024];
 File out = new File("out.apk");
 FileOutputStream fileOutputStream = new FileOutputStream(out);
 
 String str = "http://app.m.360.cn/wsjbb/download.jsp?t=22927&soft_id=22927";
 URL url = new URL(str);
 URLConnection connection = url.openConnection();
 connection.connect();
 
 InputStream is = connection.getInputStream();
 while(is.read(buff)>0){
 fileOutputStream.write(buff);
 }
 fileOutputStream.flush();
 is.close();
 fileOutputStream.close();
 
 
 } catch (Exception e) {
 e.printStackTrace();
 }
 }


恩谢谢,我试试
顶部