android文件上传

由于服务文件服务器的优化调整,导致文件上传不得不进行修改。
这是修改后的代码:
/**
 * 获取文件类型
 * @param fileUrl
 * @return
 * @throws java.io.IOException
 */
public static String getMimeType(String fileUrl)
        throws java.io.IOException
{
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String type = fileNameMap.getContentTypeFor(fileUrl);

    return type;
}
/* 上传文件至ServeruploadUrl:接收文件的处理页面 */
public static String uploadFile(String uploadUrl, String filePath) {
    String end = "\r\n";
    String twoHyphens = "--";
    String boundary = "******";
    try {
        String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
        URL url = new URL(uploadUrl);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url
                .openConnection();
        httpURLConnection.setConnectTimeout(15000);
        httpURLConnection.setReadTimeout(30000);
        // 此方法用于在预先不知道内容长度时启用没有进行内部缓冲的 HTTP 请求正文的流。
        httpURLConnection.setChunkedStreamingMode(128 * 1024);// 128K
        // 允许输入输出流
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setUseCaches(false);
        // 使用POST方法
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
        httpURLConnection.setRequestProperty("Charset", "UTF-8");
        httpURLConnection.setRequestProperty("Content-Type",
                "multipart/form-data;boundary=" + boundary);
        DataOutputStream dos = new DataOutputStream(
                httpURLConnection.getOutputStream());
        StringBuilder sb = new StringBuilder();
        /**
         * 上传文件的头
         */
        sb.append("--" + boundary + end);
        sb.append("Content-Disposition: form-data; name=\"" + "uplaodFile"
                + "\"; filename=\"" + fileName + "\"" + end);
        // 如果服务器端有文件类型的校验,必须明确指定ContentType
        sb.append("Content-Type: " + getMimeType(filePath) + end);
        sb.append("\r\n");
        byte[] headerInfo = sb.toString().getBytes("UTF-8");
        dos.write(headerInfo);

        FileInputStream fis = new FileInputStream(filePath);
        byte[] buffer = new byte[1024]; // 1k
        int count = 0;
        // 读取文件
        while ((count = fis.read(buffer)) != -1) {
            dos.write(buffer, 0, count);
        }
        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();
        return result;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

修改前的代码基本跟网上百度出来的一样:
/* 上传文件至ServeruploadUrl:接收文件的处理页面 */
public static String uploadFile(String uploadUrl, String filePath) {
    String end = "\r\n";
    String twoHyphens = "--";
    String boundary = "******";
    try {
        URL url = new URL(uploadUrl);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url
                .openConnection();
        // 设置每次传输的流大小,可以有效防止手机因为内存不足崩溃
        // 此方法用于在预先不知道内容长度时启用没有进行内部缓冲的 HTTP 请求正文的流。
        httpURLConnection.setChunkedStreamingMode(128 * 1024);// 128K
        // 允许输入输出流
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setUseCaches(false);
        // 使用POST方法
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
        httpURLConnection.setRequestProperty("Charset", "UTF-8");
        httpURLConnection.setRequestProperty("Content-Type",
                "multipart/form-data;boundary=" + boundary);

        DataOutputStream dos = new DataOutputStream(
                httpURLConnection.getOutputStream());
        dos.writeBytes(twoHyphens + boundary + end);
        dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\""
                + filePath.substring(filePath.lastIndexOf("/") + 1)
                + "\""
                + end);
        dos.writeBytes(end);

        FileInputStream fis = new FileInputStream(filePath);
        byte[] buffer = new byte[8192]; // 8k
        int count = 0;
        // 读取文件
        while ((count = fis.read(buffer)) != -1) {
            dos.write(buffer, 0, count);
        }
        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();
        return result;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
咋一看还差不多,其实还是有点差别的。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值