[Android算法] http 上传文件的方法

/**

         * 

         * sendMultipartDataToHttpServer

         * 使用post方法请求web服务器,并且当表单数据为:multipart/form-data格式。http请求使用{@link#HTTP_ENCODING}编码<br/>

         * 返回json数据,支持文件名中文上传和多文件上传,不支持断点上传,要正确编码服务端返回{@link#HTTP_ENCODING}编码<br/>

         * @param url

         * @param files 文件表单域

         * @param fields 非文件表单域

         * @return JSONObject

         * @throws Exception 

         * @exception 

         * @since  0

         */

        public static JSONObject sendMultipartDataToHttpServer(URL url,

                        final Map<String, File> files, final Map<String, String> fields,

                        final UsernamePasswordCredentials credentials) throws IOException ,JSONException,Exception{

                URL myurl = null;

                String queryString = "";

                // 其他的表单域

                if (fields != null) {

                        for (Map.Entry<String, String> entry : fields.entrySet()) {

                                queryString += "&" + URLEncoder.encode(entry.getKey(),HTTP_ENCODING) + "="

                                                + URLEncoder.encode(entry.getValue(), HTTP_ENCODING);

                        }

                }

                if (!queryString.equals("")) {

                        queryString = queryString.replaceFirst("&", "?");

                } else {

                }



                myurl = new URL(url.getProtocol(), url.getHost(),url.getPort(), url.getPath()

                                + queryString);

                HttpURLConnection conn = (HttpURLConnection) myurl.openConnection();

                conn.setConnectTimeout(UPLOAD_REQUEST_TIMEOUT);

                conn.setRequestMethod(HTTP_METHOD.POST.toString());

                conn.setDoInput(true);

                conn.setDoOutput(true);

                conn.setUseCaches(false);



                String boundary = "laohuidi_" + java.util.UUID.randomUUID().toString()

                                + "_laohuidi";

                conn.setRequestProperty(

                                                "Accept",

                                                "image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,application/vnd.ms-excel,application/vnd.ms-powerpoint,application/msword,application/x-shockwave-flash,application/x-quickviewplus,*/*");

                conn.setRequestProperty("keep-alive", "300");

                conn.setRequestProperty(

                                                "user-agent",

                                                "Mozilla/0 (Windows; U; Windows NT 2; zh-CN; rv:6) Gecko/20091201 Firefox/6 GTB6");

                conn.setRequestProperty("accept-language", "zh-cn,zh;q=5");

                conn.setRequestProperty("Connection", "Keep-Alive");

                conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+ boundary);



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

                // 乱码问题 可以试下 PrintWriter out = new PrintWriter(new

                // OutputStreamWriter(connection.getOutputStream(),"utf-8"));

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

                int bytesRead, bytesAvailable, bufferSize;

                byte[] buffer;

                int maxBufferSize = IO_BUFFER_SIZE;

                String tem = "";

                if(files!=null)

                for (Map.Entry<String, File> entry : files.entrySet()){

                        // 分隔符开头

                        dos.writeBytes(TWO_HYPHENS + boundary + LINEND);

                        // create a buffer of maximum size

                        FileInputStream fileInputStream = new FileInputStream(entry.getValue());

                        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);

                        tem = entry.getValue().getName();

                        dos.writeBytes("Content-Disposition:form-data;name=\""+entry.getKey()+"\";"+ "filename=\"");

                        dos.writeUTF(tem);// 中文的文件名使用这里

                        dos.writeBytes("\"" + LINEND);

                        dos.writeBytes("Content-Type:image/jpg" + LINEND + LINEND);//类型的判断暂时不处理

                        while (bytesRead > 0) {

                                dos.write(buffer, 0, bufferSize);

                                bytesAvailable = fileInputStream.available();

                                bufferSize = Math.min(bytesAvailable, maxBufferSize);

                                bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                        }

                        // close streams

                        fileInputStream.close();

                        dos.writeBytes(LINEND);

                }

                // http 结束符

                dos.writeBytes(TWO_HYPHENS + boundary + TWO_HYPHENS);

                dos.writeBytes(LINEND);



                dos.flush();

                dos.close();

                // 返回类型

                String responseType = conn.getHeaderField("Content-Type");

                // 正常返回而且必须为json类型

                if (conn.getResponseCode() == HttpURLConnection.HTTP_OK

                                && responseType != null

                                && responseType.indexOf(HTTP_JSON_TYPE) >= 0) {

                        responseType = (convertStreamToString(conn.getInputStream()));



                } else {

                        responseType = "{}";

                }

                try{conn.disconnect();}catch(Exception e){}

                return new JSONObject(responseType);

        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值