使用Java将大型JSON文件下载到本地文件
我正在尝试从以下URL下载JSON——下载到本地文件。我正在使用Java1.7和下面的JSON库——试图使其正常工作 这是我的密码:使用Java将大型JSON文件下载到本地文件,java,json,Java,Json,我正在尝试从以下URL下载JSON——下载到本地文件。我正在使用Java1.7和下面的JSON库——试图使其正常工作 这是我的密码: public static void download(String address, String localFileName) { OutputStream out = null; URLConnection conn = null; InputStream in = null;
public static void download(String address, String localFileName) {
OutputStream out = null;
URLConnection conn = null;
InputStream in = null;
try {
URL url = new URL(address);
out = new BufferedOutputStream(
new FileOutputStream(localFileName));
conn = url.openConnection();
in = conn.getInputStream();
byte[] buffer = new byte[1024];
int numRead;
long numWritten = 0;
while ((numRead = in.read(buffer)) != -1)
{
out.write(buffer, 0, numRead);
numWritten += numRead;
System.out.println(buffer.length);
System.out.println(" " + buffer.hashCode());
}
System.out.println(localFileName + "\t" + numWritten);
} catch (Exception exception) {
exception.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException ioe) {
}
}
}
当我运行代码时,一切似乎都正常工作,直到循环中途,程序似乎停止并不再继续读取JSON对象
有人知道为什么这会停止阅读吗?如何解决此问题?尝试以下方法:
public void saveUrl(String filename, String urlString) throws MalformedURLException, IOException
{
BufferedInputStream in = null;
FileOutputStream fout = null;
try
{
in = new BufferedInputStream(new URL(urlString).openStream());
fout = new FileOutputStream(filename);
byte data[] = new byte[1024];
int count;
while ((count = in.read(data, 0, 1024)) != -1)
{
fout.write(data, 0, count);
}
}
finally
{
if (in != null)
in.close();
if (fout != null)
fout.close();
}
}
有人知道为什么这会停止阅读吗?我如何解决这个问题
我看不出客户端代码有任何明显的错误。在客户端没有任何其他证据的情况下,我会查看服务器端日志,看看是否有任何线索
在国际海事组织,最有可能的解释是:
- 在生成JSON的服务器端代码中有一个bug,它正在中途崩溃
- 服务器(或代理/反向代理)在交互的某些部分所允许的时间上有一个超时,并且此特定请求花费的时间太长