从网上下载文件在java中
我正在使用下面的Java代码从网上下载数据。有些情况下,它工作正常。但是,有时会出现错误。可能是什么原因?从网上下载文件在java中
有一个在URL没有什么区别,因为我从他们的网站上复制它还有: 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)
我使用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();
不,它不下载文件。它只是存储HTML代码... – ELITE 2015-04-05 16:35:21
@NiRRaNjANRauT:我测试过这段代码,它对我来说工作正常。问题在于没有指定用户代理。这段代码是否给你带来任何问题? – LittlePanda 2015-04-05 16:39:53
在我的情况下,它将html代码存储到file.csv文件..... – ELITE 2015-04-05 16:49:54
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操作系统上工作。
如果这适用于您,请发布没有错误的答案。 – LittlePanda 2015-04-05 16:52:57
我添加了工作代码..... – ELITE 2015-04-05 17:13:55
我也面临着同样的问题...我认为NSE印度不支持..我通过该链接浏览器和它下载使用桌面类 – ELITE 2015-04-05 14:52:08
我有时可以获取文件。所以,NSE确实支持它。 – Raj 2015-04-05 14:56:39
我试过你的代码。我得到一个FileNotFoundException。你能否把这个例外加入你的问题?您可能需要检查返回的响应代码。它可能是404 – LittlePanda 2015-04-05 15:03:46