I'm attempting to get a bunch of pdf links from a web service and I want to give the user the file size of each link.
Is there a way to accomplish this task?
Thanks
Using a HEAD request, you can do something like this:
private static int getFileSize(URL url) {
URLConnection conn = null;
try {
conn = url.openConnection();
if(conn instanceof HttpURLConnection) {
((HttpURLConnection)conn).setRequestMethod("HEAD");
}
conn.getInputStream();
return conn.getContentLength();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if(conn instanceof HttpURLConnection) {
((HttpURLConnection)conn).disconnect();
}
}
}
The accepted answer is prone to NullPointerException, doesn't work for files > 2GiB and contains an unnecessary call to getInputStream(). Here's the fixed code:
public long getFileSize(URL url) {
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("HEAD");
return conn.getContentLengthLong();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
Update: The accepted was updated but still has issues.
Try to use HTTP HEAD method. It returns the HTTP headers only. The header Content-Length should contain information you need.
Did you try already to use getContentLength on the URL connection?
In case the server responses a valid header you should get the size of the document.
But be aware of the fact that the webserver might also return the file in chunks. In this case IIRC the content length method will return either the size of one chunk (<=1.4) or -1 (>1.4).
The HTTP response has a Content-Length header, so you could query the URLConnection object for this value.
Once the URL connection has been opened, you can try something like this:
List values = urlConnection.getHeaderFields().get("content-Length")
if (values != null && !values.isEmpty()) {
// getHeaderFields() returns a Map with key=(String) header
// name, value = List of String values for that header field.
// just use the first value here.
String sLength = (String) values.get(0);
if (sLength != null) {
//parse the length into an integer...
...
}
}
In case you are on Android, here's a solution in Java:
/**#return the file size of the given file url , or -1L if there was any kind of error while doing so*/
#WorkerThread
public static long getUrlFileLength(String url) {
try {
final HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection();
urlConnection.setRequestMethod("HEAD");
final String lengthHeaderField = urlConnection.getHeaderField("content-length");
Long result = lengthHeaderField == null ? null : Long.parseLong(lengthHeaderField);
return result == null || result < 0L ? -1L : result;
} catch (Exception ignored) {
}
return -1L;
}
And in Kotlin:
/**#return the file size of the given file url , or -1L if there was any kind of error while doing so*/
#WorkerThread
fun getUrlFileLength(url: String): Long {
return try {
val urlConnection = URL(url).openConnection() as HttpURLConnection
urlConnection.requestMethod = "HEAD"
urlConnection.getHeaderField("content-length")?.toLongOrNull()?.coerceAtLeast(-1L)
?: -1L
} catch (ignored: Exception) {
-1L
}
}
If your app is from Android N, you can use this instead:
/**#return the file size of the given file url , or -1L if there was any kind of error while doing so*/
#WorkerThread
fun getUrlFileLength(url: String): Long {
return try {
val urlConnection = URL(url).openConnection() as HttpURLConnection
urlConnection.requestMethod = "HEAD"
urlConnection.contentLengthLong.coerceAtLeast(-1L)
} catch (ignored: Exception) {
-1L
}
}
You can try this..
private long getContentLength(HttpURLConnection conn) {
String transferEncoding = conn.getHeaderField("Transfer-Encoding");
if (transferEncoding == null || transferEncoding.equalsIgnoreCase("chunked")) {
return conn.getHeaderFieldInt("Content-Length", -1);
} else {
return -1;
}
Related
I am trying to update user metadata by using HttpURLConnection PATCH API.
I had googled and found this useful link which I used.
[https://stackoverflow.com/questions/25163131/httpurlconnection-invalid-http-method-patch][1]
Following are my steps to update user metadata.
Calling database to get user information which need to be
updated, suppose database return 1000 users
.
Calling GET xxx/users/{userId} API 1000 times to check whether
database user exists or not,
Suppose GET xxx/users/{userId} API return 800 active users which
needs to be updated afterwards
Calling PATCH xxx/users/{userId} API 800 times to update user
metadata.
My code working fine if record size is less than or equal to 200-250 but If size gets increase say 1000 then application throwing exception saying
Exception is java.net.ProtocolException: Invalid HTTP method: PATCH
Here is my code.
public static void main(String[] args) throws FileNotFoundException, IOException {
// Here calling DB to get user metadata
List<CEUser> ca = getUserMetada(prop);
// Calling GET users/{userId} API to check whether user exists or not
List<CEUser> activeUsers = getActiveUsers(ca, prop);
// Calling PATCH users/{userId} API to update user metadata
updateUsername(activeUsers, prop);
}
public List<CEUser> getActiveUsers(List<CEUser> CEUsers, Properties prop) {
try {
List<CEUser> activeCEUsers = new ArrayList<>();
for (CEUser ca : CEUsers) {
URL url = new URL(prop.getCeBaseURL() + "users/" + ca.getUserId());
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setRequestMethod("GET");
httpConnection.setRequestProperty("Accept", "application/json");
httpConnection.connect();
if (httpConnection.getResponseCode() == 200)
activeCEUsers.add(ca);
httpConnection.disconnect();
}
return activeCEUsers;
} catch (Exception e) {
System.out.println("Exception occurred in getActiveUsers() method ");
}
}
private static void allowMethods(String... methods) {
try {
Field methodsField = HttpURLConnection.class.getDeclaredField("methods");
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(methodsField, methodsField.getModifiers() & ~Modifier.FINAL);
methodsField.setAccessible(true);
String[] oldMethods = (String[]) methodsField.get(null);
Set<String> methodsSet = new LinkedHashSet<>(Arrays.asList(oldMethods));
methodsSet.addAll(Arrays.asList(methods));
String[] newMethods = methodsSet.toArray(new String[0]);
methodsField.set(null/*static field*/, newMethods);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
public List<CEUser> updateUsername(List<CEUser> ceUsers, Properties prop) {
try {
allowMethods("PATCH");
List<CEUser> updatedUsername = new ArrayList<>();
for (CEUser ca : ceUsers) {
// Construct username
String username = "some static email";
// Construct email into json format to set in body part
String json = constructJson("email", username);
URL url = new URL(prop.getCeBaseURL() + "users/" + ca.getUserId());
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setRequestMethod("PATCH");
httpConnection.setDoOutput(true);
httpConnection.setRequestProperty("Accept-Charset", "UTF-8");
httpConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
try (OutputStream output = httpConnection.getOutputStream()) {
output.write(json.getBytes("UTF-8"));
}
httpConnection.connect();
if (httpConnection.getResponseCode() == 200) {
ca.setUsername(username); // set updated username
updatedUsername.add(ca);
}
httpConnection.disconnect();
}
return updatedUsername;
} catch (Exception e) {
System.out.println("Exception occurred in updateUsername() method");
}
}
Any idea why same code working for 200-250 records but not for 1000 records.
Thanks
I have a Java-method that gets a feed-document (via http) and then parses the feed (which is not of-type JSON or XML).
This is the method:
public ArrayList<ArrayList<String>> getFeed(String type)
{
String feed = "";
String address = "";
Document file;
/**
* FEED URLs-------\/
*/
switch (type) {
case "news":
address = "https://[domain]/svc/feeds/news/6001?subtree=false&imagesize=medium-square";
break;
case "events":
address = "http://[domain]/svc/feeds/events/6001?subtree=true&imagesize=medium-square&from=%5bfromDate%5d&to=%5btoDate";
}
try {
HttpURLConnection connection = (HttpURLConnection) (new URL(address)).openConnection();
//TODO: #Test
//----------------------------\/--THIS ONE WILL CAUSE ERRORS!!
file = (Document)connection.getContent();
connection.disconnect();
//OUTPUT
feed = file.getElementsByAttribute("pre").text();
stream = new StringReader(feed);
} catch (Exception e) {}
//BEGIN PARSING\\//--THEN OUTPUT//\\
try {
return parse();
} catch (FeedParseException e) {}
//de-fault
return null;
}
It's not working; saying that object:'file' caused NullPointerException.
So how do I increase my precision in debugging something which seems to me to be non-Open-Source.
P.S.: I'm not testing the "events" case so don't worry about the GET-parameters there.
here's my stack-trace:
I don't see how it helps though...
You can pass to Jsoup the URL object directly.
Instead of:
HttpURLConnection connection = (HttpURLConnection) (new URL(address)).openConnection();
//TODO: #Test
//----------------------------\/--THIS ONE WILL CAUSE ERRORS!!
file = (Document)connection.getContent();
connection.disconnect();
do
file = Jsoup //
.connect(address) //
.timeout( 10 * 1000) //
.ignoreContentType(true) //
.get();
Jsoup 1.8.3
I have two flag currently. One is remoteFlag and other is secondaryFlag. By default both of them are true, customer can change them if they want to change while they are making a call to our Application. So possible combination that can happen is -
remoteFlag secondaryFlag
true true
true false
false false
false true
By default it is always true and true.
I am supposed to make a HTTP Call to our servers (all the servers are Ubunutu machines). Suppose below are my servers with fully qualified hostname corresponding to hostname -f in unix.
String hostname1 = hostname1;
String hostname2 = hostname2;
String hostname3 = hostname3;
String hostname4 = hostname4;
I have couple of cases which I am supposed to do as described below -
Suppose if the remoteFlag and secondaryFlag both are true then I
am supposed to hit all the above servers one by one until we get the
response back from the servers. Meaning suppose hostname1 is down
so we won't get any response back from that servers and it will
throw an exception so then we will go to hostname2 to fetch the
same data but suppose if hostname2 is also down, then we will go
hostname3 to fetch the data but if suppose hostname3 is also
down then we will go to hostname4 to fetch the data. But if any of
the servers is able to give the response back then we will return
the call by making proper response saying that we have got the data.
And if none of the servers are able to give the respone back then we
will make an error response saying all the servers are down.
Now if the remoteFlag is true and secondaryFlag is false, then
we will only go to hostname1 and hostname3. If hostname1 is
down, then we will go to hostname3 to get the response. If both of
them are down, then we will make an ERROR response saying servers
are down.
Now if the remoteFlag is false and secondaryFlag is false, then
we will only go to hostname1, that's all. If hostname1 is down,
then we will make an ERROR response saying servers are down.
Now if the remoteFlag is false and secondaryFlag is true, then
we will only go to hostname1 and hostname2, that's all. If
hostname1 is down, then we will go to hostname2 to get the
response. If both of them are down, then we will make an ERROR
response saying servers are down.
By down I mean servers are not up, so that's why they are not responding. I tried on one of the servers which was down, RestTemplate throws an exception if I try to hit that server.
Below is my code so far which only works for if the remoteFlag and secondaryFlag both are true but again it doesn't look to me good code as I am simply repeating stuff in the below code. I am not sure how do I keep on calling recursively the servers if any one of them are down. But if any one of them are up and got proper response then return the call by making a SUCCESS response with the actual data that I have got.
Below is
public class ClientTask implements Callable<ClientResponse> {
public ClientTask(ClientKey clientKeys) {
this.clientKeys = clientKeys;
}
#Override
public ClientResponse call() throws Exception {
....
boolean remoteFlag = clientKeys.isRemoteFlag();
boolean secondaryFlag = clientKeys.isSecondaryFlag();
RestTemplate restTemplate = new RestTemplate();
String response = null;
...
String hostname1 = hostname1;
String hostname2 = hostname2;
String hostname3 = hostname3;
String hostname4 = hostname4;
// first use case when both are true
if(remoteFlag && secondaryFlag) {
if(hostname1 != null) {
try {
String url = generateURL(hostname1);
response = restTemplate.getForObject(url, String.class);
return new ClientResponse(response, ClientError.NONE, ClientStatus.SUCCESS);
} catch(Exception ex) {
ex.printStackTrace(); // use logger
}
}
// hostname1 is down
if(response == null && hostname2 != null) {
try {
String url = generateURL(hostname2);
response = restTemplate.getForObject(url, String.class);
return new ClientResponse(response, ClientError.NONE, ClientStatus.SUCCESS);
} catch(Exception ex) {
ex.printStackTrace(); // use logger
}
}
// hostname1 and 2 both are down
if(response == null && hostname3 != null) {
try {
String url = generateURL(hostname3);
response = restTemplate.getForObject(url, String.class);
return new ClientResponse(response, ClientError.NONE, ClientStatus.SUCCESS);
} catch(Exception ex) {
ex.printStackTrace(); // use logger
}
}
// hostname1, 2, 3 are down
if(response == null && hostname4 != null) {
try {
String url = generateURL(hostname4);
response = restTemplate.getForObject(url, String.class);
return new ClientResponse(response, ClientError.NONE, ClientStatus.SUCCESS);
} catch(Exception ex) {
ex.printStackTrace(); // use logger
}
}
}
// not sure how to add other use case logic here as well properly
}
/**
* Method to generate the url to hit the servers.
*
*/
private String generateURL(final String hostname) {
StringBuffer url = new StringBuffer();
url.append("http://" + hostname + ":8080/user?userId=" + clientKeys.getUserId() + "&page_id=" + clientKeys.getPageId());
Set<Entry<String, String>> params = clientKeys.getAttributeMap().entrySet();
for(Entry<String, String> e : params){
url.append("&" + e.getKey());
url.append("=" + e.getValue());
}
return url.toString();
}
}
ProblemStatement:-
In the above code, I am simply repeating stuff all the time, not sure how do I recursively keep on calling the servers until I get the response back. And also how do I fit other use case as well in the above code. Initially I was thinking to add same if block for other use case. Then I thought my whole code will be filled up with if block only.
Any thoughts how can I solve this problem efficiently?
You have a lot of boiler plates in your code. You can easily avoid them by only filling a list of hosts in your if statements. Afterwards you iterate over that list:
List<String> hostnames = new ArrayList<>();
if(remoteFlag && secondaryFlag) {
hostnames.add(hostname1);
hostnames.add(hostname2);
} else if …
for (String hostname : hostnames) {
if(hostname == null) {
continue;
}
try {
String url = generateURL(hostname1);
response = restTemplate.getForObject(url, String.class);
break;
} catch(Exception ex) {
ex.printStackTrace(); // use logger
}
}
if (response == null) {
// do error handling
}
return new ClientResponse(response, ClientError.NONE, ClientStatus.SUCCESS);
Personally I would prefer another solution. Build a component which expresses the state transitions and link those components:
public class FallbackCall() {
private String hostname;
private String secondaryHostname;
private FallbackQuery next;
public ClientResponse call(boolean remote, boolean secondary) {
ClientResponse response = getResponse(hostname);
if (response == null && secondary) {
reponse = getResponse(secondaryHostname);
}
if (response == null && remote) {
reponse = next.call(remote, secondary);
}
return response;
}
private ClientResponse getResponse(String hostname) {
// your boiler plate
}
}
One idea is to extract the retrieval into another method (perhaps even "static"), like so
// I don't recognize your library...
public ClientResponse call(String hostname) throws Exception {
if (hostname == null) {
return null;
}
// You might also make generateURL static
String url = generateURL(hostname);
String response = restTemplate.getForObject(url,
String.class);
if (response == null) { // add more logic here to test your response.
return null;
}
return new ClientResponse(response,
ClientError.NONE, ClientStatus.SUCCESS);
}
Then you can pass in your url(s) one by one, until you get a good response. Like this,
// String hostname1 = hostname1;
// String hostname2 = hostname2;
// String hostname3 = hostname3;
// String hostname4 = hostname4;
if(remoteFlag && secondaryFlag) {
ClientResponse response = call(hostname1);
// != null is one test. However you test the response is valid.
if (response != null) return response;
response = call(hostname2);
if (response != null) return response;
response = call(hostname3);
if (response != null) return response;
return call(hostname4);
}
I am trying to send a json string , from my BlackBerry OS < 7.X application to my Server. I am trying to use an HTTP Post request. What i have done so far is :
String httpURL = "http://ip_of_my_server/phpServer/receiver2.php/" + jsonString;
try {
HttpConnection httpConn;
httpConn = (HttpConnection) Connector.open(httpURL + getConnectionString());
httpConn.setRequestMethod(HttpConnection.POST);
httpConn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
DataOutputStream _outStream = new DataOutputStream(httpConn.openDataOutputStream());
byte[] request_body = httpURL.getBytes();
for (int i = 0; i < request_body.length; i++) {
_outStream.writeByte(request_body[i]);
}
DataInputStream _inputStream = new DataInputStream(httpConn.openInputStream());
StringBuffer _responseMessage = new StringBuffer();
int ch;
while ((ch = _inputStream.read()) != -1) {
_responseMessage.append((char) ch);
}
String res = (_responseMessage.toString());
String response = res.trim();
System.out.println("!!!!!!!!!!!!!! Response is: " + response);
httpConn.close();
} catch (Exception e) {
Dialog.alert("Error - " + e.toString());
}
The code works in a way that i dont fully understand. The author of the above code suggested to use as an httpURL the URL of the server + my json string. The final result is that on my server instead of arriving the json string , is arriving a string like that :
http://ip_of_my_server/phpServer/receiver2.php/ + jsonString
I am not familiar with java. I have previously done this for WP7 and iOS and in the httpUrl i put my servers URL and then with a command i was "appending" my json string to the http request and everything worked as expected.
How can i append the json string to the above HttpRequest , instead of adding it to the URL so that in the server arrives the JSON String only?
EDIT (providing the rest of the code that was used)
//used to specify connection type ( wifi - 3g - etc )
public static String getConnectionString() {
String connectionString = null;
// Wifi is the preferred transmission method
if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {
connectionString = ";interface=wifi";
}
// Is the carrier network the only way to connect?
else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) {
String carrierUid = getCarrierBIBSUid();
if (carrierUid == null) {
// Has carrier coverage, but not BIBS. So use the carrier's TCP network
connectionString = ";deviceside=true";
} else {
// otherwise, use the Uid to construct a valid carrier BIBS request
connectionString = ";deviceside=false;connectionUID="+carrierUid + ";ConnectionType=mds-public";
}
}
// Check for an MDS connection instead (BlackBerry Enterprise Server)
else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) {
connectionString = ";deviceside=false";
}
// If there is no connection available abort to avoid hassling the user unnecessarily
else if (CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE) {
connectionString = "none";
}
// In theory, all bases are covered by now so this shouldn't be reachable.But hey, just in case ...
else {
connectionString = ";deviceside=true";
}
System.out.println("!!!!!!!!!!!!!! Connection type is: " + connectionString);
return connectionString;
}
/**
* Looks through the phone's service book for a carrier provided BIBS
* network
*
* #return The uid used to connect to that network.
*/
private synchronized static String getCarrierBIBSUid() {
ServiceRecord[] records = ServiceBook.getSB().getRecords();
int currentRecord;
for (currentRecord = 0; currentRecord < records.length; currentRecord++) {
if (records[currentRecord].getCid().toLowerCase().equals("ippp")) {
if (records[currentRecord].getName().toLowerCase()
.indexOf("bibs") >= 0) {
return records[currentRecord].getUid();
}
}
}
return null;
}
The the first line should be simply your URL
String httpURL = "http://ip_of_my_server/phpServer/receiver2.php";
And you should only send the json string to the server as request.
instead of byte[] request_body = httpURL.getBytes();
use byte[] request_body = jsonString.getBytes();
Here is the method for OS 5.0 and above
public static HttpConnection getHttpConnection(String url, byte[] postData) {
HttpConnection conn = null;
OutputStream out = null;
try {
conn = (HttpConnection) new ConnectionFactory().getConnection(url).getConnection();
if (conn != null) {
if (postData == null) {
conn.setRequestMethod(HttpConnection.GET);
conn.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
} else {
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty("Content-Length", String.valueOf(postData.length));
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
out = conn.openOutputStream();
out.write(postData);
out.flush();
}
if (conn.getResponseCode() != 0) {
return conn;
}
}
} catch (Exception e) {
} finally {
try {
out.close();
} catch (Exception e2) {
}
}
//Only if exception occurs, we close the connection.
//Otherwise the caller should close the connection himself.
try {
conn.close();
} catch (Exception e1) {
}
return null;
}
Here is the complete class if you want it to work with OS 4.2 and above. You may need to replace the constant COVERAGE_DIRECT by its value 1, if you want to compile it with < 4.5.
public class ConnectionHelper {
/**
* Returns the working connection type. The connection types can be BIS, BES, TCP, WAP2, TCPIP
*/
public static HttpConnection getHttpConnection(String url, byte[] postData) {
int[] preferredOrder = new int[] { CONNECTION_WIFI, CONNECTION_BIS, CONNECTION_BES, CONNECTION_UNITE, CONNECTION_WAP2, CONNECTION_TCPIP, };
for (int i = 0; i < preferredOrder.length; i++) {
int type = preferredOrder[i];
if (isPresent(type)) {
HttpConnection conn = null;
OutputStream out = null;
try {
conn = (HttpConnection) Connector.open(convertURL(type, url));
if (conn != null) {
if (postData == null) {
conn.setRequestMethod(HttpConnection.GET);
conn.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
} else {
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, String.valueOf(postData.length));
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
out = conn.openOutputStream();
out.write(postData);
out.flush();
}
if (conn.getResponseCode() != 0) {
return conn;
}
}
} catch (Exception e) {
} finally {
try {
out.close();
} catch (Exception e2) {
}
}
}
}
// Only if exception occurs, we close the connection.
// Otherwise the caller should close the connection himself.
try {
conn.close();
} catch (Exception e1) {
}
return null;
}
/** Stores transport ServiceBooks if found. Otherwise, null */
private static ServiceRecord srMDS, srWiFi, srBIS, srWAP2, srUnite;
private static final int CONNECTION_DEFAULT = 0;
private static final int CONNECTION_BIS = 1;
private static final int CONNECTION_BES = 2;
private static final int CONNECTION_TCPIP = 3;
private static final int CONNECTION_WIFI = 4;
private static final int CONNECTION_WAP2 = 5;
private static final int CONNECTION_UNITE = 6;
private static final int CONFIG_TYPE_BES = 1;
private static final String UNITE_NAME = "Unite";
private static void checkTransportAvailability() {
initializeTransportAvailability();
}
/**
* Initializes the ServiceRecord instances for each transport (if available). Otherwise leaves it null. Also determines if sufficient coverage is available for each transport
* and sets coverage* flags.
*/
private static void initializeTransportAvailability() {
ServiceBook sb = ServiceBook.getSB();
ServiceRecord[] records = sb.getRecords();
for (int i = 0; i < records.length; i++) {
ServiceRecord myRecord = records[i];
String cid, uid;
if (myRecord.isValid() && !myRecord.isDisabled()) {
cid = myRecord.getCid().toLowerCase();
uid = myRecord.getUid().toLowerCase();
// BIS
if (cid.indexOf("ippp") != -1 && uid.indexOf("gpmds") != -1) {
srBIS = myRecord;
}
// BES
if (cid.indexOf("ippp") != -1 && uid.indexOf("gpmds") == -1) {
srMDS = myRecord;
}
// WiFi
if (cid.indexOf("wptcp") != -1 && uid.indexOf("wifi") != -1) {
srWiFi = myRecord;
}
// Wap2.0
if (cid.indexOf("wptcp") != -1 && uid.indexOf("wifi") == -1 && uid.indexOf("mms") == -1) {
srWAP2 = myRecord;
}
// Unite
if (getConfigType(myRecord) == CONFIG_TYPE_BES && myRecord.getName().equals(UNITE_NAME)) {
srUnite = myRecord;
}
}
}
}
/**
* Gets the config type of a ServiceRecord using getDataInt below
*
* #param record
* A ServiceRecord
* #return configType of the ServiceRecord
*/
private static int getConfigType(ServiceRecord record) {
return getDataInt(record, 12);
}
/**
* Gets the config type of a ServiceRecord. Passing 12 as type returns the configType.
*
* #param record
* A ServiceRecord
* #param type
* dataType
* #return configType
*/
private static int getDataInt(ServiceRecord record, int type) {
DataBuffer buffer = null;
buffer = getDataBuffer(record, type);
if (buffer != null) {
try {
return ConverterUtilities.readInt(buffer);
} catch (EOFException e) {
return -1;
}
}
return -1;
}
/**
* Utility Method for getDataInt()
*/
private static DataBuffer getDataBuffer(ServiceRecord record, int type) {
byte[] data = record.getApplicationData();
if (data != null) {
DataBuffer buffer = new DataBuffer(data, 0, data.length, true);
try {
buffer.readByte();
} catch (EOFException e1) {
return null;
}
if (ConverterUtilities.findType(buffer, type)) {
return buffer;
}
}
return null;
}
private static String convertURL(int connectionType, String url) {
switch (connectionType) {
case CONNECTION_BES:
url += ";deviceside=false";
break;
case CONNECTION_BIS:
url += ";deviceside=false" + ";ConnectionType=mds-public";
break;
case CONNECTION_TCPIP:
url += ";deviceside=true";
break;
case CONNECTION_WIFI:
url += ";interface=wifi";
break;
case CONNECTION_WAP2:
url += ";deviceside=true;ConnectionUID=" + srWAP2.getUid();
break;
case CONNECTION_UNITE:
url += ";deviceside=false;ConnectionUID=" + srUnite.getUid();
break;
}
return url;
}
private static boolean isPresent(int connectionType) {
checkTransportAvailability();
switch (connectionType) {
case CONNECTION_BIS:
return (srBIS != null && CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_BIS_B));
case CONNECTION_BES:
return (srMDS != null && CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_MDS));
case CONNECTION_WIFI:
return (srWiFi != null && CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_DIRECT, RadioInfo.WAF_WLAN, false));
case CONNECTION_TCPIP:
return (CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_DIRECT));
case CONNECTION_WAP2:
return (srWAP2 != null && CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_DIRECT));
case CONNECTION_UNITE:
return (srUnite != null && CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_MDS));
case CONNECTION_DEFAULT:
return true;
}
return false;
}
}
And finally post your data.
public void sendJson(String jsonString) {
String httpURL = "http://ip_of_my_server/phpServer/receiver2.php";
HttpConnection httpConn = null;
try {
httpConn = getHttpConnection(httpURL, jsonString.getBytes());
if(httpConn.getResponseCode() == 200) {
//If you need the output, then read it. Otherwise comment it.
byte[] data = IOUtilities.streamToBytes(httpConn.openInputStream());
String response = new String(data);
System.out.println("!!!!!!!!!!!!!! Response is: " + response);
}
} catch (Exception e) {
}
finally {
try {
httpConn.close();
} catch (Exception e2) {
}
}
}
I have got this piece of code (I didn't write, just maintaining):
public class MyMultipartResolver extends CommonsMultipartResolver{
public List parseEmptyRequest(HttpServletRequest request) throws IOException, FileUploadException {
String contentType = request.getHeader(CONTENT_TYPE);
int boundaryIndex = contentType.indexOf("boundary=");
InputStream input = request.getInputStream();
byte[] boundary = contentType.substring(boundaryIndex + 9).getBytes();
MultipartStream multi = new MultipartStream(input, boundary);
multi.setHeaderEncoding(getHeaderEncoding());
ArrayList items = new ArrayList();
boolean nextPart = multi.skipPreamble();
while (nextPart) {
Map headers = parseHeaders(multi.readHeaders());
// String fieldName = getFieldName(headers);
String subContentType = getHeader(headers, CONTENT_TYPE);
if (subContentType == null) {
FileItem item = createItem(headers, true);
OutputStream os = item.getOutputStream();
try {
multi.readBodyData(os);
} finally {
os.close();
}
items.add(item);
} else {
multi.discardBodyData();
}
nextPart = multi.readBoundary();
}
return items;
}
}
I am using commons-fileupload.jar version 1.2.1 and obviously the code is using some deprecated methods...
Anyway, while trying to use this code to upload a very large file (780 MB) I get this:
org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
at org.apache.commons.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:983)
at org.apache.commons.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:887)
at java.io.InputStream.read(InputStream.java:89)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:94)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:64)
at org.apache.commons.fileupload.MultipartStream.readBodyData(MultipartStream.java:593)
at org.apache.commons.fileupload.MultipartStream.discardBodyData(MultipartStream.java:619)
that is thrown from 'multi.discardBodyData();' line.
My question:
How can I avoid this error and be able to be able to succeed collecting the FileItems?
catch
(org.apache.commons.fileupload.MultipartStream.MalformedStreamException e)
{
e.printStackTrace();
return ERROR;
}
Catch the exception and handle it via ..either InputStream or Return Error use it in struts action tag