0

我正在使用以下代码从 sd 卡发送文件。

 HttpURLConnection conn = null;
         DataOutputStream dos = null;
         DataInputStream inStream = null;
         String existingFileName = Environment.getExternalStorageDirectory().getAbsolutePath() 
                 + "/111";

     File f = new File(existingFileName);
     String lineEnd = "\r\n";
     String twoHyphens = "--";
     String boundary =  "*****";
     int bytesRead, bytesAvailable, bufferSize;
     byte[] buffer;
     int maxBufferSize = 1*1024*1024;
     String responseFromServer = "";
     String urlString = "http://192.178.1.7/Geo/Prodect(filename)";
     try
     {
      //------------------ CLIENT REQUEST
     FileInputStream fileInputStream = new FileInputStream(f);
      // open a URL connection to the Servlet
      URL url = new URL(urlString);
      // Open a HTTP connection to the URL
      conn = (HttpURLConnection) url.openConnection();
      // Allow Inputs
      conn.setDoInput(true);
      // Allow Outputs
      conn.setDoOutput(true);
      // Don't use a cached copy.
      conn.setUseCaches(false);
      // Use a post method.
      conn.setRequestMethod("POST");
      conn.setRequestProperty("Connection", "Keep-Alive");
      conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
      dos = new DataOutputStream( conn.getOutputStream() );
      dos.writeBytes(twoHyphens + boundary + lineEnd);
      dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + existingFileName + "\"" + lineEnd);
      dos.writeBytes(lineEnd);
      // create a buffer of maximum size
      bytesAvailable = fileInputStream.available();
      bufferSize = Math.min(bytesAvailable, maxBufferSize);
      buffer = new byte[bufferSize];
      // read file and write it into form...
      bytesRead = fileInputStream.read(buffer, 0, bufferSize);
      //while (bytesRead > 0)
      Log.v("info",".size."+bytesRead);
      for(int n1=0;n1<bytesRead;n1++)
      {


       dos.write(buffer, 0, bufferSize);
       bytesAvailable = fileInputStream.available();
       bufferSize = Math.min(bytesAvailable, maxBufferSize);
       bytesRead = fileInputStream.read(buffer, 0, bufferSize);

      }

      dos.writeBytes(lineEnd);
      dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
      // close streams
      Log.v("info","File is written");
      fileInputStream.close();
      dos.flush();
      dos.close();
     }
     catch (MalformedURLException ex)
     {
          Log.v("info", "error: " + ex.getMessage(), ex);
     }
     catch (IOException ioe)
     {
          Log.v("info", "error: " + ioe.getMessage(), ioe);
     }
     //------------------ read the SERVER RESPONSE
     try {
           inStream = new DataInputStream ( conn.getInputStream() );
           String str;

           while (( str = inStream.readLine()) != null)
           {
                Log.v("info","Server Response "+str);
           }
           inStream.close();

     }
     catch (IOException ioex)
     {
          Log.v("info", "error: " + ioex.getMessage(), ioex);
     }

它工作正常,但该文件包含 20 行,它保存在服务器中,共 22 行。

我的实际文件如下所示。

android 应用信息 应用名称 应用时间

当我将文件从 sd 卡发送到服务器时,文件如下所示

1) ** mnt/sdcard/111 **

android 应用信息 应用名称 应用时间

2) **

添加了 1 和 2 行。

我如何在没有这两行的情况下发送文件。

如果有人知道解决方案,请帮助我。

提前致谢..

4

2 回答 2

1

尝试评论以下内容

  dos.writeBytes(twoHyphens + boundary + lineEnd);
      dos.writeBytes(lineEnd);
于 2012-11-12T06:39:12.247 回答
1
public static StringBuffer post(String url,InputStream in) throws IOException {
        InputStream bufferInputStream = null;
        InputStreamReader responseInputStream = null;
        HttpURLConnection conn = null;
        OutputStream requestOutputStream = null;
        StringBuffer responseString = new StringBuffer();
        int bufferSize = 8192;
        byte[] byteBuffer  = new byte[bufferSize];
        int postDataSize = 0;
        try {
            if (in != null) {
                bufferInputStream = new BufferedInputStream(in);
            }
            if (bufferInputStream != null) {
                postDataSize = bufferInputStream.available();
            }
            //send request
            conn = getConnecttion(url);
            if (postDataSize > 0) {
                requestOutputStream = conn.getOutputStream();
                int position = 0;

                while ((position = bufferInputStream.read(byteBuffer)) > -1) {
                    requestOutputStream.write(byteBuffer, 0, position);
                }
                requestOutputStream.flush();
                requestOutputStream.close();
                requestOutputStream = null;
                byteBuffer = null;
            }
            // get response
            int responseCode = conn.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                responseInputStream = new InputStreamReader(conn.getInputStream(),"UTF-8");

                char[] charBuffer = new char[bufferSize];
                int _postion = 0;
                while ((_postion=responseInputStream.read(charBuffer)) > -1) {
                    responseString.append(charBuffer,0,_postion);
                }
                responseInputStream.close();
                responseInputStream = null;
                charBuffer = null;
            }else{
                throw new IOException("Respsone code:"+responseCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new IOException(e.getMessage());
        } finally {
            byteBuffer = null;

            if (responseInputStream != null) {
                responseInputStream.close();
                responseInputStream = null;
            }
            if (conn != null) {
                conn.disconnect();
                conn = null;
            }
            if (requestOutputStream != null) {
                requestOutputStream.close();
                requestOutputStream = null;
            }
            if (bufferInputStream != null) {
                bufferInputStream.close();
                bufferInputStream = null;
            }
        }
        return responseString;
    }

请参考上面的简单代码,

像这样发送文件:post("http://youurl.com",new FileInputStream("you file path"))

于 2012-11-12T07:09:04.313 回答