Android文件上传错误

时间:2013-11-17 11:07:51

标签: android upload

我长时间一直在拉头发。

我在最后一行收到错误..

错误是:

  

此行有多个标记      - 期望的令牌“uploaded_file”上的语法错误      - 双引号

未正确关闭字符串文字

任何帮助?

  

public int uploadFile(String sourceFileUri){

      String fileName = sourceFileUri;

      HttpURLConnection conn = null;
      DataOutputStream dos = null; 
      String lineEnd = "\r\n";
      String twoHyphens = "--";
      String boundary = "*****";
      int bytesRead, bytesAvailable, bufferSize;
      byte[] buffer;
      int maxBufferSize = 1 * 1024 * 1024;
      File sourceFile = new File(sourceFileUri);

      if (!sourceFile.isFile()) {

           dialog.dismiss();

           Log.e("uploadFile", "Source File not exist :"
                               +uploadFilePath + "" + uploadFileName);

           runOnUiThread(new Runnable() {
               public void run() {
                   messageText.setText("Source File not exist :"
                           +uploadFilePath + "" + uploadFileName);
               }
           });

           return 0;

      }
      else
      {
           try {

                 // open a URL connection to the Servlet
               FileInputStream fileInputStream = new FileInputStream(sourceFile);
               URL url = new URL(upLoadServerUri);

               // Open a HTTP  connection to  the URL
               conn = (HttpURLConnection) url.openConnection();
               conn.setDoInput(true); // Allow Inputs
               conn.setDoOutput(true); // Allow Outputs
               conn.setUseCaches(false); // Don't use a Cached Copy
               conn.setRequestMethod("POST");
               conn.setRequestProperty("Connection", "Keep-Alive");
               conn.setRequestProperty("ENCTYPE", "multipart/form-data");
               conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
               conn.setRequestProperty("uploaded_file", fileName);

               dos = new DataOutputStream(conn.getOutputStream());

               dos.writeBytes(twoHyphens + boundary + lineEnd);
               dos.writeBytes("Content-Disposition: form-data; name="uploaded_file";filename=""
                                         + fileName + "" + lineEnd);

1 个答案:

答案 0 :(得分:0)

您需要使用反斜杠转义字符串中的双引号:

dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"\" + fileName + \"\"" + lineEnd);