Download from an URL in Spring [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I want that , by pasting the URL of a file (the file can be an image, an Xhtml, or a Css) into a form of a JSP, this can be downloaded form internet and saved locally. Please can you help me ?

you can use this to open URL in the browser and save into the file location.
<%
String site= contain the string(URL);
response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader("Location", site);
File file = new File("/Users/asdf.xml");
FileWriter fr = null;
BufferedWriter br = null;
URL url = new URL(site);
BufferedReader reader = new BufferedReader
(new InputStreamReader(url.openStream()));
fr = new FileWriter(file);
br = new BufferedWriter(fr);
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
br.write(line);
br.newLine();
}
reader.close();
br.close();
%>

Related

Http request incomplete [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am trying to make an HTTP request for a personal project and for some reason my request did not return the full HTML.
I don't know if I'm forgetting anything, I tried googling it but I can't find anything that helped.
URL link = new Url("https:\\www (...)");
HttpURLConnection con = (HttpURLConnection)link.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine = "";
while (in.readLine() != null){
inputLine += in.readLine();
}
in.close();
con.disconnect();
I was expecting the full HTML page, but I got part of it, like:
HTML Page:
<div>
<span>product</span>
<span>price</span>
</div>
received:
<div>
<span>product</span>
</div>
Think about what this does:
String inputLine = "";
while (in.readLine() != null){
inputLine += in.readLine();
}
You're checking if a line does not equal null, then appending the next line to the string. This means you skip every other line.
To fix this, you can assign a temporary variable the value of the nextLine() call and use that instead:
String inputLine = "";
String line = in.readLine();
while (line != null){
inputLine += line;
line = in.readLine();
}
Inline:
String inputLine = "";
String line;
while ((line = in.readLine()) != null){
inputLine += line;
}

BufferedReader/Writer not working in Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I'm trying to make two basic functions which will allow me to call them from other classes in order to return a HashMap from the 'getAllData()' and to write to the file in 'writeToFile()', without any luck. I've been tampering with it for a while now and just getting a multitude of strange errors.
Code:
static HashMap<Integer ,String> getAllData(Integer choice) throws Exception{
InputStream localInputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("Shadow.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(localInputStream));
if (choice.equals(1)){
while ((!data.equals(null))){
data = br.readLine();
dataString = dataString+data;
}
writeToFile(dataString);
br.close();
}return name;
}
static void writeToFile(String data) throws IOException{;
File file = new File ("Shadow.txt");
FileWriter fileWriter = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fileWriter);
bw.write(data);
bw.newLine();
bw.flush();
bw.close();
}
With this current code, nothing happens. The file remains exactly how it is, although to me, the code should read everything from it, and then append it.
How can I fix this?
Thanks
This might help you:
static void getAllData(final Integer choice) throws Exception {
final BufferedReader br = new BufferedReader(new FileReader("Shadow.txt"));
String data = "";
final StringBuilder builder = new StringBuilder();
if(choice.equals(1)) {
while(data != null) {
data = br.readLine();
if(data != null) {
builder.append(data + "\n");
}
}
writeToFile(builder.toString());
br.close();
}
}
static void writeToFile(final String data) throws IOException {
final File file = new File("Shadow.txt");
final FileWriter fileWriter = new FileWriter(file, true);
final BufferedWriter bw = new BufferedWriter(fileWriter);
bw.write(data);
bw.flush();
bw.close();
}

Trouble opening text file [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have some trouble reading a file. I have my input.txt in contents package, but the program still can't open the file.
String line = null;
try{
//loen faili
FileReader fileReader = new FileReader("contents/input.txt");
BufferedReader buffReader = new BufferedReader(fileReader);
while((line = buffReader.readLine()) != null){
System.out.println(line);
}
buffReader.close();
}catch(FileNotFoundException ex){
System.out.println("Error opening file");
}catch(IOException ex){
System.out.println("Error reading file");
}
Because you need to differnetly open files from your app packages than the one from disk, try:
InputStream is = getClass().getClassLoader().getResourceAsStream("contents/input.txt");

How to get the page source from specific link in Java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Is it possible to get the inputStream of particular website content or it's page source into String?
For instance, I want to download the whole html tag from particular website into string or xml. Is it possible?
Yes of course you just have to do something like
public static void main(String[] args) {
URL url;
try {
// get URL content
url = new URL("http://www.mkyong.com");
URLConnection conn = url.openConnection();
// open the stream and put it into BufferedReader
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
//save to this filename
String fileName = "/users/mkyong/test.html";
File file = new File(fileName);
if (!file.exists()) {
file.createNewFile();
}
//use FileWriter to write file
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
while ((inputLine = br.readLine()) != null) {
bw.write(inputLine);
}
bw.close();
br.close();
System.out.println("Done");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
CREDIT : mkyong
You may want to look at guava's CharStreams class.
CharStreams.toString(new InputStreamReader(..))
will save you from writing much boilerplate code.
Here is doc

How can i find the City name using IP address in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want city name from IP address using Java
is there any idea to do this ?
From Andrey link, here is how to construct the inquiry, this code will return an HTML file with all details of current IP, including city;
String IP= "123.123.123.123";
URL link = new URL("http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress="+IP);
BufferedReader in = new BufferedReader(new InputStreamReader(link.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null){
System.out.println(inputLine);
}
in.close();
UPDATE, 23 May 2013
The previous answer is ok, but it's not an API call, it reads an HTML page, that I provided previously because I didn't find any free APIs. Next is a REST API call that can be used easily and will return all the info required, it's recommended to use this one:
String ip = "2.51.255.200";
URL url = new URL("http://freegeoip.net/csv/" + ip);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
int status = connection.getResponseCode();
if (status != 200) {
return null;
}
reader = new BufferedReader(new InputStreamReader(is));
for (String line; (line = reader.readLine()) != null;) {
//this API call will return something like:
"2.51.255.200","AE","United Arab Emirates","03","Dubai","Dubai","","x-coord","y-coord","",""
// you can extract whatever you want from it
}
If your Application is deployed behind the firewall. So instead of calling a API, we can use GeoLite below is the sample code.
Download City.dat file from http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
File datapath = new File("GeoLiteCity.dat");
LookupService cl = new LookupService(datapath,
LookupService.GEOIP_MEMORY_CACHE
| LookupService.GEOIP_CHECK_CACHE);
String cityName = cl.getLocation(ipAddress).city;

Categories

Resources