android上传文件至服务器

public class UploadService {

     public void uploadFile(String filePath,OnUploadProgressListener progress){

         File uploadFile=newFile(filePath);

         if(uploadFile==null||uploadFile.length()<=0){

             progress.onError("文件为空!");

             return;

         }

         long fileSize=uploadFile.length();

           String uploadUrl = "http://192.168.0.106:8080/upload_file_service/upload.jsp";

           String end = "\r\n";

           String twoHyphens = "--";

           String boundary = "******";

           try{

             URL url = new URL(uploadUrl);

             HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();

             httpURLConnection.setDoInput(true);

             httpURLConnection.setDoOutput(true);

             httpURLConnection.setUseCaches(false);

             httpURLConnection.setChunkedStreamingMode(1024*50);//分块传输

            //httpURLConnection.setFixedLengthStreamingMode((int)fileSize);//采用流式的输出

             httpURLConnection.setRequestMethod("POST");

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

             httpURLConnection.setRequestProperty("Charset", "UTF-8");

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

             DataOutputStream dos = newDataOutputStream(httpURLConnection.getOutputStream());

             dos.writeBytes(twoHyphens + boundary + end);

             dos.writeBytes("Content-Disposition:form-data; name=\"file\"; filename=\""

                 + filePath.substring(filePath.lastIndexOf("/")+ 1)+ "\""+ end);

             dos.writeBytes(end);

             FileInputStream fis = new FileInputStream(filePath);

             

             byte[] buffer = new byte[1024]; // 8k

             int count = 0;

             int currentUploadSize =0;

             while ((count = fis.read(buffer)) != -1){

               dos.write(buffer, 0, count);

               currentUploadSize+=count;

               progress.onUploadProgress(fileSize, currentUploadSize);

             }

             fis.close();

             dos.writeBytes(end);

             dos.writeBytes(twoHyphens + boundary + twoHyphens + end);

             dos.flush();

             InputStream is = httpURLConnection.getInputStream();

             InputStreamReader isr = new InputStreamReader(is, "utf-8");

             BufferedReader br = new BufferedReader(isr);

             String result = br.readLine();

             dos.close();

             is.close();

             progress.onComplete(result);

           } catch (Exception e){

             e.printStackTrace();

           }

         }

     

     public interface OnUploadProgressListener {

          void onUploadProgress(longfileSize,long progress);

          void onComplete(String msg);

          void onError(String msg);

     }

     

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值