我使用以下Java代码从网上下载数据。有些情况下工作正常。但是,有时会出错。可能是什么原因?
网址没有区别,因为我也从网站上复制了网址: http://www.nseindia.com/content/equities/scripvol/datafiles/07-04-2010-TO-01-04-2011TCSALLN.csv
我的网址是: http://www.nseindia.com/content/equities/scripvol/datafiles/07-04-2010-TO-01-04-2011TCSALLN.csv
所以,我认为没有区别。
错误:未找到:此服务器上不存在请求的对象。您遵循的链接要么过时,不准确,要么已指示服务器不要让您拥有它。
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class AutomatedDownloader
{
private static FileOutputStream fos;
public static void main(String args[]) throws IOException, ParseException
{
String combo = "TCS";
String userDate = "02-04-2011";
Date date1, date2, date3;
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
Calendar cal = Calendar.getInstance();
date1 = formatter.parse(userDate);
cal.setTime(date1);
cal.add(Calendar.DATE, -1);
date2 = cal.getTime();
String endDate = df.format(date2);
System.out.println(endDate);
cal.setTime(date1);
cal.add(Calendar.DATE, - 360);
date3 = cal.getTime();
String startDate = df.format(date3);
System.out.println(startDate);
String url ="http://www.nseindia.com/content/equities/scripvol/datafiles/"+startDate+"-TO-"+endDate+combo+"ALLN.csv";
String fileName = "C:\\Users\\Parin\\Documents\\ParinAndroid\\StockPredictor\\DownloadedData\\"+combo+".csv";
URL website = new URL(url);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
fos = new FileOutputStream(fileName);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
System.out.print("done");
}
异常:
Exception: Exception in thread "main" java.io.FileNotFoundException: http://www.nseindia.com/content/equities/scripvol/datafiles/07-12-2013-TO-01-12-2014TCSALLN.csv
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1243)
at java.net.URL.openStream(URL.java:1009)
at Predictor.AutomatedDownloader.main(AutomatedDownloader.java:46)
答案 0 :(得分:0)
我使用Jsoup库获取了该文件。下载Jsoup jar文件并将其添加到类路径中。这是代码:
<强>进口强>:
import java.io.FileOutputStream;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
<强>代码强>:
//Open a URL Stream
Response resultImageResponse = Jsoup
.connect("http://www.nseindia.com/content/equities/scripvol/datafiles/07-04-2010-TO-01-04-2011TCSALLN.csv")
.ignoreContentType(true)
.userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0")
.execute();
// output here
FileOutputStream out = (new FileOutputStream(new java.io.File("" + "file.csv")));
out.write(resultImageResponse.bodyAsBytes());
out.close();
答案 1 :(得分:0)
public static void main(String args[]) {
String index = "cnx nifty junior";
String from_date = "01-03-2015";
String to_date = "20-03-2015";
download(index, from_date, to_date);
}
public static void download(String index, String from_date, String to_date) {
String link = "http://www.nseindia.com/products/dynaContent/equities/indices/historicalindices.jsp?indexType=" + index.replaceAll(" ", "%20").toUpperCase() + "&fromDate=" + from_date + "&toDate=" + to_date;
try {
URL url = new URL(link);
URLConnection connection = url.openConnection();
connection.setReadTimeout(5000);
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder html = new StringBuilder();
String line = reader.readLine();
while (line != null) {
html.append(line);
line = reader.readLine();
}
reader.close();
Document document = Jsoup.parse(html.toString());
Elements elements = document.getElementsByTag("a");
Iterator<Element> iterator = elements.iterator();
String downloadLink = null;
if (iterator.hasNext()) {
Element element = iterator.next();
downloadLink = "http://www.nseindia.com" + element.attr("href");
}
if (downloadLink != null) {
Runtime rt = Runtime.getRuntime();
rt.exec("rundll32 url.dll,FileProtocolHandler " + downloadLink);
}
} catch (Exception ex) {
ex.printStackTrace(System.err);
}
}
下载成功。您需要添加jsoup.jar库。但它只适用于Windows操作系统。