将图像上传到服务器时的ServerResponse 404

时间:2017-07-08 10:04:44

标签: android httpclient androidhttpclient

我想通过Android将Image上传到Perticular Url(www.myUrl.com/myFolderName/)并检索该网址的地址。

这是我的代码

class SaveFile extends AsyncTask<String,String,String>
{
    FileInputStream fileInputStream;
    HttpURLConnection connection;
    URL url;
    DataOutputStream outputStream;
    int bytesRead,bytesAvailable,bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;
    File selectedFile = new File(selectedImagePath);
    //selectedImagePath = path of Image in phonememory
    @Override
    protected String doInBackground(String... params)
    {
        Log.v(TAG,"Image path is  " + selectedImagePath);

        try
        {
            fileInputStream = new FileInputStream(selectedFile);
            url = new URL(SEND_IMAGE);
            connection= (HttpURLConnection)url.openConnection();
            Log.v(TAG,"connection established");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Connection","Keep-Alive");
            connection.setRequestProperty("ENCTYPE","multipart/form-data");
            connection.setRequestProperty("Content-Type","multipart/form-data;boundary=*****");
            //connection.setRequestProperty("image1",selectedImagePath);
            //connection.setRequestProperty("user_id","1");
            outputStream = new DataOutputStream(connection.getOutputStream());

            outputStream.writeBytes("--*****\r\n");
            outputStream.writeBytes("Content-Disposition:form-data;name=\"uploaded_file\";filename=\""+selectedImagePath+"\""+"\r\n");
            outputStream.writeBytes("\r\n");
            //outputStream.writeBytes("--*****\r\n");
            bytesAvailable = fileInputStream.available();
            bufferSize =  Math.min(bytesAvailable,maxBufferSize);
            buffer = new byte[bufferSize];

            bytesRead = fileInputStream.read(buffer,0,bufferSize);
            while (bytesRead>0)
            {
                outputStream.write(buffer,0,bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable,maxBufferSize);
                bytesRead = fileInputStream.read(buffer,0,bufferSize);
            }
            outputStream.writeBytes("\r\n");
            outputStream.writeBytes("--*****--\r\n");

            int  serverResponseCode = connection.getResponseCode();
            String serverResponseMessage = connection.getResponseMessage();

            if(serverResponseCode  == HttpURLConnection.HTTP_OK) {
                String line;
                StringBuilder sb = new StringBuilder();
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
                Log.v(TAG,"Response is " +sb.toString());

            }
            Log.v("UploadImage", "Server Response is: " + serverResponseMessage + ": " + serverResponseCode);
            fileInputStream.close();
            outputStream.flush();
            outputStream.close();
        }
        catch (Exception e)
        {
            Log.v("UploadImage"," " + e.toString());
        }
        return null;
    }


    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);


    }
}

但我得到 serverResponse 404

我尝试了像Uploading Image to Server - Android这样的解决方案,但没有奏效。

我搜索了很多。但是找不到解决方案

请帮助!!

1 个答案:

答案 0 :(得分:0)

我想说这可能是服务器错误。在大多数情况下,404是一个错误,上面写着“您使用了错误的网址”。检查此网址是否正确。请在浏览器中上传您的数据。