网上相似的代码一大堆,但都是同一个问题————从服务器返回来的值都报空指针异常的错误,然后我就在我一个只可以传文件的代码上逐渐添加上了请求参数的代码,结果一次成功!!
话不多说上代码
public static String uploadFile(String path,Map<String,Object> params,File file) throws IOException {
String uuid = UUID.randomUUID().toString();
String BOUNDARY = uuid;
String NewLine = "\r\n";
StringBuffer result = new StringBuffer();
String PREFIX = "--", LINEND = "\r\n";
String CHARSET = "UTF-8";
String spec = path;
FileInputStream fis=null;
DataOutputStream bos =null;
DataInputStream bis=null;
URL url = new URL(spec);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//打开输出
connection.setDoOutput(true);
//打开输入
connection.setDoInput(true);
//关闭缓存
connection.setUseCaches(false);
//读取超时
connection.setReadTimeout