使用HttpUrlConnection进行post请求上传文件

使用HttpUrlConnection模拟post表单进行文件上传平时很少使用,比较麻烦。

 

原理是: 分析文件上传的数据格式,然后根据格式构造相应的发送给服务器的字符串。

格式如下:这里的httppost123是我自己构造的字符串,可以是其他任何的字符串

----------httppost123 (\r\n)
Content-Disposition: form-data; name="img"; filename="t.txt" (\r\n)
Content-Type: application/octet-stream (\r\n)

(\r\n)

sdfsdfsdfsdfsdf (\r\n)
----------httppost123 (\r\n)
Content-Disposition: form-data; name="text" (\r\n)

(\r\n)

text tttt (\r\n)
----------httppost123-- (\r\n)
(\r\n)

 

上面的(\r\n)表示各个数据必须以(\r\n)结尾。

 

具体Java代码如下:

 

 

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

public class HttpPostUtil {
	URL url;
	HttpURLConnection conn;
	String boundary = "--------httppost123";
	Map<String, String> textParams = new HashMap<String, String>();
	Map<String, File> fileparams = new HashMap<String, File>();
	DataOutputStream ds;

	public HttpPostUtil(String url) throws Exception {
		this.url = new URL(url);
	}
    //重新设置要请求的服务器地址,即上传文件的地址。
	public void setUrl(String url) throws Exception {
		this.url = new URL(url);
	}
    //增加一个普通字符串数据到form表单数据中
	public void addTextParameter(String name, String value) {
		textParams.put(name, value);
	}
    //增加一个文件到form表单数据中
	public void addFileParameter(String name, File value) {
		fileparams.put(name, value);
	}
    // 清空所有已添加的form表单数据
	public void clearAllParameters() {
		textParams.clear();
		fileparams.clear();
	}
    // 发送数据到服务器,返回一个字节包含服务器的返回结果的数组
	public byte[] send() throws Exception {
		initConnection();
		try {
			conn.connect();
		} catch (SocketTimeoutException e) {
			// something
			throw new RuntimeException();
		}
		ds = new DataOutputStream(conn.getOutputStream());
		writeFileParams();
		writeStringParams();
		paramsEnd();
		InputStream in = conn.getInputStream();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int b;
		while ((b = in.read()) != -1) {
			out.write(b);
		}
		conn.disconnect();
		return out.toByteArray();
	}
    //文件上传的connection的一些必须设置
	private void initConnection() throws Exception {
		conn = (HttpURLConnection) this.url.openConnection();
		conn.setDoOutput(true);
		conn.setUseCaches(false);
		conn.setConnectTimeout(10000); //连接超时为10秒
		conn.setRequestMethod("POST");
		conn.setRequestProperty("Content-Type",
				"multipart/form-data; boundary=" + boundary);
	}
    //普通字符串数据
	private void writeStringParams() throws Exception {
		Set<String> keySet = textParams.keySet();
		for (Iterator<String> it = keySet.iterator(); it.hasNext();) {
			String name = it.next();
			String value = textParams.get(name);
			ds.writeBytes("--" + boundary + "\r\n");
			ds.writeBytes("Content-Disposition: form-data; name=\"" + name
					+ "\"\r\n");
			ds.writeBytes("\r\n");
			ds.writeBytes(encode(value) + "\r\n");
		}
	}
    //文件数据
	private void writeFileParams() throws Exception {
		Set<String> keySet = fileparams.keySet();
		for (Iterator<String> it = keySet.iterator(); it.hasNext();) {
			String name = it.next();
			File value = fileparams.get(name);
			ds.writeBytes("--" + boundary + "\r\n");
			ds.writeBytes("Content-Disposition: form-data; name=\"" + name
					+ "\"; filename=\"" + encode(value.getName()) + "\"\r\n");
			ds.writeBytes("Content-Type: " + getContentType(value) + "\r\n");
			ds.writeBytes("\r\n");
			ds.write(getBytes(value));
			ds.writeBytes("\r\n");
		}
	}
    //获取文件的上传类型,图片格式为image/png,image/jpg等。非图片为application/octet-stream
	private String getContentType(File f) throws Exception {
		
//		return "application/octet-stream";  // 此行不再细分是否为图片,全部作为application/octet-stream 类型
		ImageInputStream imagein = ImageIO.createImageInputStream(f);
		if (imagein == null) {
			return "application/octet-stream";
		}
		Iterator<ImageReader> it = ImageIO.getImageReaders(imagein);
		if (!it.hasNext()) {
			imagein.close();
			return "application/octet-stream";
		}
		imagein.close();
		return "image/" + it.next().getFormatName().toLowerCase();//将FormatName返回的值转换成小写,默认为大写

	}
    //把文件转换成字节数组
	private byte[] getBytes(File f) throws Exception {
		FileInputStream in = new FileInputStream(f);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		byte[] b = new byte[1024];
		int n;
		while ((n = in.read(b)) != -1) {
			out.write(b, 0, n);
		}
		in.close();
		return out.toByteArray();
	}
	//添加结尾数据
	private void paramsEnd() throws Exception {
		ds.writeBytes("--" + boundary + "--" + "\r\n");
		ds.writeBytes("\r\n");
	}
	// 对包含中文的字符串进行转码,此为UTF-8。服务器那边要进行一次解码
    private String encode(String value) throws Exception{
    	return URLEncoder.encode(value, "UTF-8");
    }
	public static void main(String[] args) throws Exception {
		HttpPostUtil u = new HttpPostUtil("http://localhost:3000/up_load");
		u.addFileParameter("img", new File(
				"D:\\素材\\圆月.jpg"));
		u.addTextParameter("text", "中文");
		byte[] b = u.send();
		String result = new String(b);
		System.out.println(result);

	}

}

 

 

后台使用ruby进行接收

ruby代码如下:

require "cgi"
class UpLoadController < ApplicationController
	protect_from_forgery :except=>:index
	
	def index
		img = params[:img]
		if img.kind_of? String
			logger.debug "img string : #{img}"
		else
			logger.debug "Content-Type:#{img.content_type}"
			logger.debug "or:#{CGI.unescape(img.original_filename)}"
		end
		text = params[:text]
		logger.debug "text:#{CGI.unescape(text)}"
		render :text=>"OK"
	end
end

 

日志输入如下:

 Content-Type:image/jpeg
or:圆月.jpg
text:中文

 

如果不把中文转成UTF-8的格式进行传输,则后台显示中文乱码。

同样,如果其他参数包含中文,则也应当先转码。

当然,具体什么编码要和后台接收的编码一致。

 

 

 另外附上c#的代码,因为对c#不太熟悉,但代码测试基本可以实现了。

using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Drawing.Imaging;

namespace MyThreading
{
    class HttpPostUtil
    {
        string boundary = "------httpost123";
        string url;
        byte[] bs= new byte[0];
        Encoding encoder = Encoding.UTF8;
        public HttpPostUtil(string url)
        {
            this.url = url;
        }
        public byte[] Send()
        {
            WebClient myWebClient = new WebClient();
            myWebClient.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundary);
            EndData();
            return myWebClient.UploadData(url,bs); 
        }
        public void ClearData()
        {
            Array.Clear(bs, 0, bs.Length);
        }
        public void AddTextParameter(string name, string value)
        {
            StringBuilder s = new StringBuilder();
            s.Append("--").Append(boundary).Append("\r\n");
            s.Append("Content-Disposition:  form-data;  name=\"" + name + "\"\r\n");
            s.Append("\r\n");
            s.Append(value).Append("\r\n");
            AppendString(s.ToString());
        }
        public void AddFileParameter(string name, FileInfo file)
        {
            StringBuilder s = new StringBuilder();
            s.Append("--").Append(boundary).Append("\r\n");
            s.Append("Content-Disposition:  form-data;  name=\"" + name + "\";  filename=\"" + file.Name + "\"\r\n");
            s.Append("Content-Type: " + GetContentType(file) + "\r\n");
            s.Append("\r\n");
            AppendString(s.ToString());
            AppendBytes(GetFileBytes(file));
            AppendString("\r\n");     
        }
        byte[] GetFileBytes(FileInfo f)
        {
            FileStream fs = new FileStream(f.FullName, FileMode.Open);
            BinaryReader br = new BinaryReader(fs);
            byte[] b = br.ReadBytes((int)f.Length);
            br.Close();
            fs.Close();
            return b;
        }
        string GetContentType(FileInfo f)
        {
          // return "application/octet-stream";  //取消注释此行,则不再区分是否为图片
            System.Drawing.Image image = null;
            FileStream fs = new FileStream(f.FullName, FileMode.Open);
            try
            {
                image = System.Drawing.Image.FromStream(fs);
            }
            catch (ArgumentException)
            {
                // 文件无法被解析为一个图片
                return "application/octet-stream";
            }
            finally 
            {
                fs.Close();
            }
            return  GetFormat(image.RawFormat);
        }
        string GetFormat(ImageFormat imf) 
        {
            if (ImageFormat.Bmp.Equals(imf))
            {
                return "image/bmp";
            }
            if (ImageFormat.Jpeg.Equals(imf)) 
            {
                return "image/jpeg";
            }
            if (ImageFormat.Png.Equals(imf)) 
            {
                return "image/png";
            }
            return "application/octet-stream";
        }
        void AppendBytes(byte[] bytes) 
        {
            byte[] newByte = new byte[bs.Length + bytes.Length];
            Array.Copy(bs, 0, newByte, 0, bs.Length);
            Array.Copy(bytes, 0, newByte, bs.Length, bytes.Length);
            bs = newByte;
        }
        void AppendString(string value)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(value);
            AppendBytes(bytes);
        }
        void EndData()
        {
            StringBuilder s = new StringBuilder();
            s.Append("--").Append(boundary).Append("--\r\n");
            s.Append("\r\n");
            AppendString(s.ToString());
        }
    }
}

在我本机测试,中文没有出现乱码。

c#调用方法:

 

           string url = "http://localhost:3000/up_load";
           string imgPath = @"D:\素材\圆月.jpg";
           HttpPostUtil hp = new HttpPostUtil(url);
           hp.AddTextParameter("text","中文");
           hp.AddFileParameter("img",new FileInfo(imgPath));
           byte[] b = hp.Send();
            Console.WriteLine(Encoding.UTF8.GetString(b));

 

如有错误,请补充下。

使用 HttpURLConnection 上传文件时,需要设置请求方法为 POST,并且在请求头中添加 Content-Type 和 Content-Disposition 字段,指定上传文件的类型和文件名。同时,需要在请求体中添加表单数据和文件内容。以下是一个示例代码: ``` URL url = new URL("http://example.com/upload"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); String boundary = "*****"; // 自定义边界字符串 conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); OutputStream outputStream = conn.getOutputStream(); PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"), true); // 添加表单数据 writer.append("--" + boundary).append("\r\n"); writer.append("Content-Disposition: form-data; name=\"username\"").append("\r\n"); writer.append("\r\n"); writer.append("john").append("\r\n"); writer.flush(); // 添加文件内容 writer.append("--" + boundary).append("\r\n"); writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"example.txt\"").append("\r\n"); writer.append("Content-Type: text/plain; charset=UTF-8").append("\r\n"); writer.append("\r\n"); writer.flush(); Files.copy(Paths.get("example.txt"), outputStream); outputStream.flush(); // 结束请求 writer.append("\r\n").append("--" + boundary + "--").append("\r\n"); writer.close(); // 发送请求并读取响应 int statusCode = conn.getResponseCode(); if (statusCode == HttpURLConnection.HTTP_OK) { InputStream inputStream = conn.getInputStream(); // 处理响应数据 } ``` 其中,boundary 字符串需要自定义,用于分隔不同的表单项。在添加表单数据和文件内容时,需要按照指定格式添加,并且在添加文件内容时需要使用 `Files.copy` 方法将文件内容写入到输出流中。最后,要记得关闭输出流和输入流,并根据响应状态码来处理响应数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值