首页  >  问答  >  正文

图片上传 - Android上传图片到Java服务端出现的问题

我在Android端上传图片的时候URL如果使用的内网IP,用内部网络上传图片的时候是没有问题的,但是如果我Android端URL使用外网IP的话,只能使用公司内部网络才能正确上传,用自己流量或其他外部网络只能上传几十k的图片,上传大一点的就上传不了。之前觉得可能是外网有什么限制,但是今天在家通过服务端的测试页面(如下图)上传图片,也成功了,所以感觉外网应该也没有什么限制,实在不知道是什么原因了,有大神知道吗?急求答案,谢谢!试过用Socket上传,也是一样的情况,会出现一样的问题。

Android端代码:

public class HttpUpLoadImageUtil {
    static String BOUNDARY = java.util.UUID.randomUUID().toString();
    static String PREFIX = "--", LINEND = "\r\n";
    static String MULTIPART_FROM_DATA = "multipart/form-data";
    static String CHARSET = "UTF-8";

    public static void doPostPicture(String urlStr, Map<String,Object> paramMap, File pictureFile )
            throws Exception{

        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setDoInput(true);// 允许输入
        conn.setDoOutput(true);// 允许输出
        conn.setUseCaches(false);
        conn.setReadTimeout(10 * 1000); // 缓存的最长时间
        conn.setRequestMethod("POST");

        conn.setRequestProperty("Charset", "UTF-8");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY);


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

        StringBuilder sb = new StringBuilder(); //用StringBuilder拼接报文,用于上传图片数据
        sb.append(PREFIX);
        sb.append(BOUNDARY);
        sb.append(LINEND);
        sb.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + pictureFile.getName() + "\"" + LINEND);
        sb.append("Content-Type: image/jpg; charset=" + CHARSET + LINEND);
        sb.append(LINEND);
        os.write(sb.toString().getBytes());
        InputStream is = new FileInputStream(pictureFile);

        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = is.read(buffer)) != -1) {
            os.write(buffer, 0, len); //写入图片数据
        }
        is.close();

        os.write(LINEND.getBytes());

        StringBuilder text = new StringBuilder();
        for(Map.Entry<String,Object> entry : paramMap.entrySet()) { //在for循环中拼接报文,上传文本数据
            text.append("--");
            text.append(BOUNDARY);
            text.append("\r\n");
            text.append("Content-Disposition: form-data; name=\""+ entry.getKey() + "\"\r\n\r\n");
            text.append(entry.getValue());
            text.append("\r\n");
        }
        os.write(text.toString().getBytes("utf-8")); //写入文本数据

        // 请求结束标志
        byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
        os.write(end_data);
        os.flush();
        os.close();

        // 得到响应码
        int res = conn.getResponseCode();
        System.out.println("asdf code "+ res);
        System.out.println("asdf " + conn.getResponseMessage());
        conn.disconnect();
    }
}
大家讲道理大家讲道理2685 天前670

全部回复(1)我来回复

  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-06-12 09:21:48

    能成功的话代码出问题的可能性是不大的,如果要是出错了至少需要得到错误信息才能排查。

    回复
    0
  • 取消回复