java - 黑莓手机无法上传大于1KB的文件

标签 java blackberry java-me file-upload

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;
            }

    }

最佳答案

我认为,你必须调用“dos.flush()”方法。我的代码运行完美,postContent 是字符串缓冲区并包含 300KB 字符串。

        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();
            }

关于java - 黑莓手机无法上传大于1KB的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5645161/

相关文章:

java-me - 有没有办法为支持 Java ME 的设备开发新的 JSR?

java - 将Java程序的输出结果保存到文件中

java - 我可以在不硬编码大小的情况下将 ArrayList 中的 Java 值分配给不同的变量吗?

java - 使用 pom-packaging maven 项目作为依赖

android - 移动 Web 开发框架

java - 是否可以在 J2me 中使用 java SE 库?

java - 如何修复非静态变量 this 无法从具有内部类的静态类引用?

java - Blackberry - 如何实现ListField平滑滚动?

c++ - BB10 - 启用 Qml 调试。仅在安全环境中使用

iphone - 针对不同移动平台的开发工作