2 sleepheader SleepHeader 于 2016.04.20 19:36 提问

关于Okhttp无法上传图片到django 服务器上,哪位大神知道怎么回事,烦请告知! 1C
 private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("application/x-jpg");
public static final MediaType MEDIA_TYPE_MP4 = MediaType.parse("video/mp4");

MultipartBuilder builder = new MultipartBuilder("*****").type(MultipartBuilder.FORM);
for (final PhotoThumbnail thumbnail : dataSet) {
MediaType type = thumbnail.getType() == PhotoThumbnail.TYPE_PICTURE ? MEDIA_TYPE_PNG : MEDIA_TYPE_MP4;
builder.addFormDataPart("upload", null, RequestBody.create(type, new File(thumbnail.getPath())));
}

    RequestBody requestBody = builder.build(); // be carefor build() must be after addFormDataPart();
    final Request request = new Request.Builder()
            .url(url)//地址
            .post(requestBody)//添加请求体
            .build();
    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            System.out.println("request = " + request.urlString());

        }

        @Override
        public void onResponse(Response response) throws IOException 
            String responseContent = response.body().string();// must be care for only can read one
            try {
                JSONObject object = new JSONObject(responseContent);
                String result = object.getString("result");
                String code = object.getString("error_code");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });

            经过测试,在JavaEE服务器上可以正确上传图片,但是在django 服务器上却无法正确获取图片,后来把客户端改成
             /* 上传文件至Server的方法 */
private static void uploadFile(String path)
{
    String end ="\r\n";
    String twoHyphens ="--";
    String boundary ="*****";
    try
    {
        URL url =new URL(ParameterConstant.BASE_URL);
        HttpURLConnection con=(HttpURLConnection) url.openConnection();
      /* 允许Input、Output,不使用Cache */
        con.setDoInput(true);
        con.setDoOutput(true);//允许输出流,即允许上传
        con.setUseCaches(false); //不使用缓冲
      /* 设置传送的method=POST */
        con.setRequestMethod("POST");
      /* setRequestProperty */
        con.setRequestProperty("Connection", "Keep-Alive");
        con.setRequestProperty("Charset", "UTF-8");
        con.setRequestProperty("Content-Type",
                "multipart/form-data;boundary="+boundary);
      /* 设置DataOutputStream */
        DataOutputStream ds =
                new DataOutputStream(con.getOutputStream());
        ds.writeBytes(twoHyphens + boundary + end);
        ds.writeBytes("Content-Disposition: form-data; "+
                "name=\"file1\";filename=\""+
                newName +"\""+ end);
        ds.writeBytes(end);
      /* 取得文件的FileInputStream */
        FileInputStream fStream =new FileInputStream(path);
      /* 设置每次写入1024bytes */
        int bufferSize =1024;
        byte[] buffer =new byte[bufferSize];
        int length =-1;
      /* 从文件读取数据至缓冲区 */
        while((length = fStream.read(buffer)) !=-1)
        {
        /* 将资料写入DataOutputStream中 */
            ds.write(buffer, 0, length);
        }
        ds.writeBytes(end);
        ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
      /* close streams */
        fStream.close();
        ds.flush();
      /* 取得Response内容 */
        InputStream is = con.getInputStream();
        int ch;
        StringBuffer b =new StringBuffer();
        while (( ch = is.read() ) !=-1 )
        {
            b.append( (char)ch );
        }

        ds.close();
    }
    catch(Exception e)
    {

    }
}

后,django 服务器端就可以正常获取图片了,哪位大神知道怎么回事吗?okhttp 哪里我用错了吗?不胜感激~~

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!