android 文件上传类

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MyHttpClient {
    private URL url;
    private String host="";
    private String referer="";
    private HttpURLConnection con;
    private OutputStream os;
    private String delimiter = "--";
    private String boundary =  "APP"+Long.toString(System.currentTimeMillis())+"ANDROID";
    public MyHttpClient(String url) throws MalformedURLException {                
        this.url=new URL(url);
        host=this.url.getHost();
        referer=url;
    }
    public void connect4Multipart() throws Exception {        
        con = (HttpURLConnection) url.openConnection();        
        con.setRequestMethod("POST");
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setUseCaches(false);
        con.setRequestProperty("Connection", "Keep-Alive");
        con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);        
        con.setRequestProperty("Charset", "iso-8859-1");
        con.setRequestProperty("connection", "keep-alive");        
        con.setRequestProperty("Referer", referer);
        con.setRequestProperty(
                "User-Agent",
                "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.6) Gecko/20100625   Firefox/3.6.6 Greatwqs");        
        con.setRequestProperty("Accept-Language", "zh-cn,zh;q=0.5");
        con.setRequestProperty("Host", host);
        con.setRequestProperty("Accept-Charset",
                "ISO-8859-1,utf-8;q=0.7,*;q=0.7");        
        con.connect();        
        os = con.getOutputStream();
    }

    public void addFormPart(String paramName, String value) throws Exception {
        writeParamData(paramName, value);
    }    

    public void addFilePart(String paramName, String fileName, String path) throws Exception {
        os.write( (delimiter + boundary + "\r\n").getBytes());
        os.write( ("Content-Disposition: form-data; name=\"" + paramName +  "\"; filename=\"" + fileName + "\"\r\n"  ).getBytes());
        os.write( ("Content-Type: application/octet-stream\r\n"  ).getBytes());
        os.write( ("Content-Transfer-Encoding: binary\r\n"  ).getBytes());
        os.write("\r\n".getBytes());
        File f = new File(path);
        FileInputStream fis=new FileInputStream(f);
        byte[] buf=new byte[1024];
        while(true){
            int len=fis.read(buf);
            if(len>0){
                os.write(buf, 0, len);
            }else{
                break;
            }
        }
        fis.close();
        os.write("\r\n".getBytes());
    }

    public void finishMultipart() throws Exception {
        os.write( (delimiter + boundary + delimiter + "\r\n").getBytes());
        os.flush();
        os.close();
    }
   
    
    public String getResponse() throws Exception {
        InputStream is = con.getInputStream();
        byte[] b1 = new byte[1024];
        StringBuilder buffer = new StringBuilder();        
        while ( is.read(b1) != -1){
            buffer.append(new String(b1));
        }        
        con.disconnect();
        String rst=buffer.toString();
        return rst;
    }
   
    private void writeParamData(String paramName, String value) throws Exception {        
        os.write( (delimiter + boundary + "\r\n").getBytes());
        os.write( "Content-Type: text/plain\r\n".getBytes());
        os.write( ("Content-Disposition: form-data; name=\"" + paramName + "\"\r\n").getBytes());;
        os.write( ("\r\n" + value + "\r\n").getBytes());        
    }
}

 

转载于:https://my.oschina.net/jingshishengxu/blog/675579

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值