Java代码从此URL下载数据

时间:2014-09-21 20:54:08

标签: java url download

所以我是java编程的新手,我应该每隔5分钟从URL下载数据。

主持人:204.8.38.210

得到:?/Iowa.Sims.AllSites.C2C/IADOT_SIMS_AllSites_C2C.asmx/OP_ShareTrafficDetectorData MSG_TrafficDetectorDataRequest =字符串%20HTTP / 1.1

我收到此错误 - "服务器返回HTTP响应代码:400"使用下面的代码

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class JavaDownload {
    public static void main(String[] args) throws IOException{
        String fileName = "file3x.html";

        URL link = new URL("http://204.8.38.210/Iowa.Sims.AllSites.C2C/IADOT_SIMS_AllSites_C2C.asmx/OP_ShareTrafficDetectorData?MSG_TrafficDetectorDataRequest=string%20HTTP/1.1");

        InputStream in = new BufferedInputStream(link.openStream());
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int n=0;

        while(-1!=(n=in.read(buf)))
        {
            out.write(buf,0,n);
        }
        out.close();
        in.close();
        byte[] response = out.toByteArray();

        FileOutputStream fos = new FileOutputStream(fileName);
        fos.write(response);
        fos.close();
        System.out.println("Finished");

    }

}

0 个答案:

没有答案