Java: Can you parse a file ON server? - java

I always understood that in order to read a file on the server you have to download it first ie:
URL url = new URL(myUrl);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
input = connection.getInputStream();
output = new FileOutputStream(TEMP_FILE_PATH);
byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
// allow canceling with back button
if (isCancelled()) {
input.close();
return null;
}
output.write(data, 0, count);
}
Is my assumption incorrect? Can you read / parse a file without downloading it?

Is it possible to parse a file ON server?
Yes, if your parser is running on the server. If your parser isn't running with direct access to the file then you have to fetch the file somehow to parse it.

Is it possible to parse a file ON server?
Definitely YES you can.
Based on your code snippet, you seem to want to read and parse a file content from a remote server or http request.
I have an application in which user can preview a file from a remote file server.
If you can access a file using "myUrl" directly, you can also read and parse the file in java.
Please try to use the code snippet below.
You may need to include org.apache.http.client.HttpClient library.
HTTP GET Example
String myUrl = "http://enter.the.url.you.want";
OutputStream output = new FileOutputStream("TEMP_FILE_PATH");
// Define Header information if you want to have.
Map<String, String> headers = new HashMap<String, String>();
headers.put("range", "bytes=0-51199");
// org.apache.http.client.HttpClient
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(myUrl);
if(headers != null){
Iterator<String> iter = headers.keySet().iterator();
while(iter.hasNext()){
String key = (String)iter.next();
get.addHeader(key, headers.get(key));
}
}
HttpResponse res = client.execute(get);
InputStream input = res.getEntity().getContent();
int count = 0;
byte[] data = new byte[4096];
StringBuilder sb = new StringBuilder();
while ((count = input.read(data)) != -1) {
sb.append(new String(data, 0, count, "UTF-8"));
// Here is the place you can read a file.
output.write(data);
}
input.close();
output.close();
HTTP POST Example
String myUrl = "http://enter.the.url.you.want";
OutputStream output = new FileOutputStream("TEMP_FILE_PATH");
// Define parameters if you want to have.
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("key", "some-value"));
// Define Header information if you want to have.
Map<String, String> headers = new HashMap<String, String>();
headers.put("range", "bytes=0-51199");
// org.apache.http.client.HttpClient
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(myUrl);
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
if(headers != null){
Iterator<String> iter = headers.keySet().iterator();
while(iter.hasNext()){
String key = (String)iter.next();
post.addHeader(key, headers.get(key));
}
}
HttpResponse res = client.execute(post);
InputStream input = res.getEntity().getContent();
int count = 0;
byte[] data = new byte[4096];
StringBuilder sb = new StringBuilder();
while ((count = input.read(data)) != -1) {
sb.append(new String(data, 0, count, "UTF-8"));
// Here is the place you can read a file.
output.write(data);
}
input.close();
output.close();

Related

Set multiple content type in handling get request

I have created a http server which handles the request and send JSON as well as exe file as response to the get request. So can we set multiple content types in one request?
Headers h = new Headers();
h = t.getResponseHeaders();
JSONObject json = new JSONObject();
json.put("version", dirFiles.lastEntry().getKey());
String output = json.toString(); // I want to send this with response
File file = new File(fileName);
h.add("CONTENT-TYPE", "application/octet-stream");
FileInputStream fs = new FileInputStream(file);
final byte[] buffer = new byte[4096];
int count = 0;
while ((count = fs.read(buffer)) >= 0) {
os.write(buffer, 0, count);
}
fs.close();
os.close();
This would require a multipart/mixed content. Each part would have a separate content type, for example application/octet-stream.

Incorrect Json String was received on client while it was generated correctly on server side

I am working on a Server-Client application. For part of the requests, I need to generate a piece of Json String on Server side and send it to Client. The Json String was generated correctly on the server side according to the Server log. But on the Android client side, the String was changed and cannot be parsed as correct Json String.
Here are some related code.
Generate Json String on Server side:
#RequestMapping(value="/resourceList/{actType}/{type}/{id}")
#ResponseBody public Object personList(#PathVariable int actType, #PathVariable int type, #PathVariable int id){
ArrayList<ItemBase> list = new ArrayList();
......
return new ArrayList();
}
This generates following Json code:
[{"countryID":1,"locationID":5,"siteID":5,"brief":"shark point","userID":0,"status":"normal","rank":0.0,"id":2,"timestamp":1471494991000,"displayName":"shark point","pic":"2.jpg","actType":1,"type":64},{"countryID":1,"locationID":5,"siteID":5,"brief":"halik","userID":0,"status":"normal","rank":0.0,"id":3,"timestamp":1471495034000,"displayName":"halik","pic":"3.jpg","actType":1,"type":64}]
And receive it on Android client:
......
HttpURLConnection urlConnection = null;
try {
URL url = new URL(request);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
byte[] buffer = new byte[256];
int len = 0;
StringBuilder responseBuilder = new StringBuilder();
while ((len = in.read(buffer)) != -1) {
String str = new String(buffer, "utf-8");
responseBuilder.append(str);
}
String response = responseBuilder.toString().trim();
The response variable was written with value:
[{"countryID":1,"locationID":5,"siteID":5,"brief":"halik","userID":0,"status":"normal","rank":0.0,"id":3,"timestamp":1471495034000,"displayName":"halik","pic":"3.jpg","actType":1,471494991000,"displayName":"shark point","pic":"2.jpg","actType":1,"type":64},{"countryID":1,"locationID":5,"siteID":5,"brief":"halik","userID":0,"status":"normal","rank":0.0,"id":3,"timestamp":1471495034000,""type":64}]":"halik","pic":"3.jpg","actType":1,471494991000,"displayName":"shark point","pic":"2.jpg","actType":1,"type":64},{"countryID":1,"locationID":5,"siteID":5,"brief":"halik","userID":0,"status":"normal","rank":0.0,"id":3,"timestamp":1471495034000,"
Which cannot be parsed as Json String correctly with obvious errors.
Most methods which return a Json String to client request work fine as I expected except this one. But this method was implemented almost exactly the same as those ones work correctly. Thus I have no idea how this happened at all. Any one got any clew please help.
You're building String the wrong way.
Try this instead:
// …
HttpURLConnection urlConnection = null;
try {
URL url = new URL(request);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedInputStream bis = new BufferedInputStream(in);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result = bis.read();
while(result != -1) {
buf.write((byte) result);
result = bis.read();
}
String response = buf.toString();
// …

Decoding HttpEntity into android string - encoding issue

I know that should be basics but i had no formation :( and I don't understand it, everywhere it seems obvious to people. I get that one side encode data with his set and android is probably expecting another one, but what can I do to translate?
My app perform a get request on google maps api to retrieve an address from a Lat/lng. But I failed to decode properly the result as a French è is displayed as è
I have not enough xp in Java to understand what to do. It is linked with UTF-8, right?
What should I do?
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
JSONObject jsonObject = new JSONObject();
jsonObject = new JSONObject(stringBuilder.toString());
retList = new ArrayList<Address>();
if("OK".equalsIgnoreCase(jsonObject.getString("status"))){
JSONArray results = jsonObject.getJSONArray("results");
for (int i=0;i<results.length();i++ ) {
JSONObject result = results.getJSONObject(i);
String indiStr = result.getString("formatted_address");
Address addr = new Address(Locale.ITALY);
addr.setAddressLine(0, indiStr);
Dbg.d(TAG, "adresse :"+addr.toString());
retList.add(addr);
}
}
Thanks for help !
Try using UTF-8,
instead of using InputStream try something like,
String responseBody = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
you can use BufferReader
your code will be like this:
InputStream stream = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
int b;
while ((b = br.read()) != -1) {
stringBuilder.append(b);
}

Open remote file and write to it

I open a txt file on my server, get the Int and want to increment the int by 1 and write it to the file again.
I get the file with this method:
public int getCount() {
try {
URL updateURL = new URL("http://myserver.gov/text.txt");
URLConnection conn = updateURL.openConnection();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while((current = bis.read()) != -1){
baf.append((byte)current);
}
/* Convert the Bytes read to a String. */
String tmp = new String(baf.toByteArray());
int count = Integer.valueOf(tmp);
return count;
} catch(Exception e) {
Log.e(TAG, "getAdCount Exception = " + e);
e.printStackTrace();
return -1;
}
}
now I simply increment the count and want to write it to the file.
I figured out, that it is possible to write to a file with this method:
BufferedWriter out = new BufferedWriter(new FileWriter("text.txt"));
out.write(count);
out.close();
But how I open the remote file? I dont find a way. Thanks!
##### Edit: #####
I have written this code:
URL url = new URL("http://myserver.gov/text.txt");
URLConnection con = url.openConnection();
con.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
out.write(count);
out.close();
But it doesnt write the count to the file.
When you want to work with URLConnection you can follow the instructions here: Reading from and Writing to a URLConnection.
Update: You will also need a running server handling POST requests to update your counter.
According to me .When you are open remote file.Firstly you have to open connection than read file content.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
than you can write file content.

Get contents of an online file and assign it to a variable

How do I retrieve the contents of a file and assign it to a string?
The file is located on a https server and the content is plain text.
I suggest Apache HttpClient: easy, clean code and it handles the character encoding sent by the server -- something that java.net.URL/java.net.URLConnection force you to handle yourself:
String url = "http://example.com/file.txt";
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(new HttpGet(url));
String contents = EntityUtils.toString(response.getEntity());
Look at the URL Class in the Java API.
Pretty sure all you need is there.
First download the file from the server using the URL class of java.
String url = "http://url";
java.io.BufferedInputStream in = new java.io.BufferedInputStream(new
java.net.URL(url).openStream());
java.io.FileOutputStream fos = new java.io.FileOutputStream("file.txt");
java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
byte data[] = new byte[1024];
while(in.read(data,0,1024)>=0)
{
bout.write(data);
}
bout.close();
in.close();
Then read the downloaded file using FileInputStream class of java
File file = new File("file.txt");
int ch;
StringBuffer strContent = new StringBuffer("");
FileInputStream fin = null;
try {
fin = new FileInputStream(file);
while ((ch = fin.read()) != -1)
strContent.append((char) ch);
fin.close();
} catch (Exception e) {
System.out.println(e);
}
System.out.println(strContent.toString());
Best answer I found:
public static String readPage(String url, String delimeter)
{
try
{
URL URL = new URL(url);
URLConnection connection = URL.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line, lines = "";
while ((line = reader.readLine()) != null)
{
if(lines != "")
{
lines += delimeter;
}
lines += line;
}
return lines;
}
catch (Exception e)
{
return null;
}
}

Categories