七牛直传,报400,下面是我的方法:
private static final String LINE_END = "\r\n";
public String postFileWithParameters(String urlStr, Map<String, String> fileMap,
Map<String, String> paraMap) throws Exception {
// creates a unique boundary based on time stamp
String boundary = "<" + System.currentTimeMillis() + ">";
URL url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setConnectTimeout(TIMEOUT_MILLIS);
connection.setReadTimeout(TIMEOUT_MILLIS);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
OutputStream outputStream = connection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, CHARSET),
true);
if (fileMap != null) {
Iterator<Map.Entry<String, String>> iter = fileMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, String> entry = iter.next();
String inputName = entry.getKey();
String inputValue = entry.getValue();
if (inputValue == null) {
continue;
}
File file = new File(inputValue);
String fileName = file.getName();
writer.append("--" + boundary).append(LINE_END);
writer.append(
"Content-Disposition: form-data; name=\"" + inputName
-
"\"; filename=\"" + fileName + "\"")
.append(LINE_END); writer.append( "Content-Type: application/octet-stream") .append(LINE_END); writer.append("Content-Transfer-Encoding: binary").append(LINE_END); writer.flush(); FileInputStream inputStream = new FileInputStream(file); byte[] buffer = new byte[4096];
bytesRead = -1;
-
((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead); } outputStream.flush(); inputStream.close(); writer.append(LINE_END); writer.flush(); } }
-
(paraMap != null) {
Iterator<Map.Entry<String, String>> iter = paraMap.entrySet().iterator(); while (iter.hasNext()) { Map.Entry<String, String> entry = iter.next(); String paraKey = entry.getKey(); String paraValue = entry.getValue(); writer.append("--" + boundary).append(LINE_END); writer.append("Content-Disposition: form-data; name=\"" + paraKey + "\"") .append(LINE_END); writer.append("Content-Type: text/plain; charset=" + CHARSET).append( LINE_END); writer.append(paraValue).append(LINE_END); writer.flush(); } } writer.append("--" + boundary + "--").append(LINE_END).flush(); writer.close(); LogX.d(urlStr, connection.getResponseCode() + "");
-
(connection.getResponseCode()) {
case HttpURLConnection.HTTP_OK: case HttpURLConnection.HTTP_CREATED: StringBuilder sb = new StringBuilder(); BufferedReader br = new BufferedReader( new InputStreamReader(connection.getInputStream(), "utf-8")); String line = null; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); LogX.d("返回", sb.toString()); connection.disconnect(); return sb.toString(); case HttpURLConnection.HTTP_UNAUTHORIZED: connection.disconnect(); throw new IllegalArgumentException(connection.getResponseMessage()); case HttpURLConnection.HTTP_NOT_FOUND: connection.disconnect(); throw new Exception(connection.getResponseMessage()); case HttpURLConnection.HTTP_BAD_REQUEST: connection.disconnect(); throw new IllegalArgumentException(connection.getResponseMessage()); case HttpURLConnection.HTTP_FORBIDDEN: connection.disconnect(); JsonObject jo = new JsonObject(); jo.addProperty(JSON_KEY_ERROR, ERROR_FORBIDDEN); return jo.toString(); default: connection.disconnect(); throw new Exception(connection.getResponseMessage()); } }
下面是我的传值:
urlStr = "http://upload.qiniu.com"; HashMap<String,String> fileMap = new HashMap<>(); fileMap.put("file", mPhotoPath); HashMap<String,String> paraMap = new HashMap<>(); paraMap.put("token", token); paraMap.put("key",key);
上传报文有问题,肯定是不推荐自己写的,java-SDK里面都有这些东西功能,一两行代码搞定:
http://developer.qiniu.com/docs/v6/sdk/java-sdk.html
具体400报错,肯定就是请求报文有问题,我是建议你抓个包看下,就是你实际上传只是提交一个http请求,而且你实现的功能是表单上传。然后你抓包看一下你的发送报文是不是跟表单的一致,最好可以把表单贴出来,跟文档中的对比一下:
估计是漏了什么或者格式不对。