unable to upload file great than 1KB in blackberry
private final class ServerConnectThread2 extends Thread
{
private InputStreamReader _in=null;
private OutputStreamWriter _out=null;
private String _url;
private int _delay;
private String _key;
private String _connectionParameters;
OutputStream os=null;
FileConnection fconn=null;
ServerConnectThread2()
{
_key="";
}
public void run()
{
// check internet connection available
setConnectionStringParameter();
bulkUploaderFlag=true;
开发者_开发知识库 HttpConnection connection = null;
try
{
_url="http://xxx/bulkuploader.jsp";
_url= EncodeURL (_url) ;
_url = _url + _connectionParameters;
URLEncodedPostData _postData = new URLEncodedPostData("",false);
_postData.append("totalRecord",""+totalRecord);
int count=0;
while( count < totalRecord)
{
_postData.append("accountID"+count,"C"+(String)accountIDVector.elementAt(count));
_postData.append("deviceID"+count,(String)deviceIDVector.elementAt(count));
_postData.append("timestamp"+count,(String)timestampVector.elementAt(count));
_postData.append("statusCode"+count,(String)statusCodeVector.elementAt(count));
_postData.append("latitude"+count,(String)latitudeVector.elementAt(count));
_postData.append("longitude"+count,(String)longitudeVector.elementAt(count));
_postData.append("gpsAge"+count,(String)gpsAgeVector.elementAt(count));
_postData.append("speedKPH"+count,(String)speedKPHVector.elementAt(count));
_postData.append("heading"+count,(String)headingVector.elementAt(count));
_postData.append("altitude"+count,(String)altitudeVector.elementAt(count));
_postData.append("transportID"+count,(String)transportIDVector.elementAt(count));
//_postData.append("inputMask",""+0);
count++;
}
byte [] postDataBytes = _postData.getBytes();
// Open the connection
connection = (HttpConnection)Connector.open(_url, Connector.READ_WRITE, false);
// Set the request method as GET
connection.setRequestMethod("POST");
//connection.setRequestMethod(HttpConnection.POST);
connection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.0");
connection.setRequestProperty("Content-Language", "en-US");
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
//connection.setRequestProperty("Content-Type","application/octet-stream");
//connection.setRequestProperty("Content-Type","multipart/form-data");
String len =new String(postDataBytes);
System.out.println(len);
connection.setRequestProperty("Content-Length",""+len.length());
os= connection.openOutputStream();
os.write(postDataBytes);
exceptionLogObj.saveLog(" [ "+System.currentTimeMillis() +", sct2 ]");
int rc = connection.getResponseCode();
if(rc == HttpConnection.HTTP_OK)
{
_in = new InputStreamReader(connection.openInputStream());
// Wait for an acknowledgment, in this case an '1' character, for 'Received'.
//char c = (char)_in.read();
int length=0;
exceptionLogObj.saveLog(" [ "+System.currentTimeMillis() +", sct2, http==OK ]\n");
StringBuffer buf = new StringBuffer();
while ((length = _in.read()) != -1) {
char c = (char)length;
buf.append(c);
if(c=='0')
{
exceptionLogObj.saveLog(" [ "+System.currentTimeMillis() +", sct2, c==0,not send in bulk unsucessfully ]\n");
System.out.println("DATA not send in bulk SUCCESSFULLY sct2, c==0");
break;
}
if(c=='1')
{
exceptionLogObj.saveLog(" [ "+System.currentTimeMillis() +", sct2, c==1, send in bulk successfully ]\n");
System.out.println("DATA send in bulk SUCCESSFULLY sct2");
String fullPath = "file:///SDCard/dataLog.txt";
try
{
fconn = (FileConnection) Connector.open(fullPath, Connector.READ_WRITE);
if (fconn.exists())
fconn.delete();
}
catch(Exception e){}
finally
{
if(fconn!=null)
fconn.close();
}
break;
}
}
}
else
{
System.out.println("DATA not send in bulk SUCCESSFULLY sct2");
exceptionLogObj.saveLog(" [ "+System.currentTimeMillis() +", sct2, http not ok ]\n");
}
}
catch (Exception e)
{
exceptionLogObj.saveLog(" [ "+System.currentTimeMillis() +", sct2, exception catch ]\n");
exceptionLogObj.saveLog(" [ "+System.currentTimeMillis() + ", "+e.getMessage()+" ] \n");
System.out.println("DATA not send in bulk SUCCESSFULLY"+e.getMessage());
}
finally
{
try
{
if(_in!=null)
_in.close();
if(_out!=null)
_out.close();
if(os!=null)
os.close();
if(connection!=null)
connection.close();
bulkUploaderFlag=false;
}
catch (IOException ioe)
{
// Do Nothing
}
return;
}
}
I think , you must call "dos.flush()" method. My code runs perfectly, postContent is string buffer and contanins 300KB string.
if (getPostContent() == null) {
httpConnection.setRequestMethod(HttpConnection.GET);
} else {
String type = "application/x-www-form-urlencoded";
byte[] bytes = postContent.toString().getBytes("UTF-8");
httpConnection.setRequestProperty("Content-Type", type);
httpConnection.setRequestProperty("Content-Length", bytes.length + "");
httpConnection.setRequestMethod(HttpConnection.POST); //setupPost method for this conn
DataOutputStream dos = new DataOutputStream(httpConnection.openOutputStream());
dos.write(bytes);
dos.flush();
dos.close();
}
精彩评论