我正在使用' MultipartUtil'在SO question的第一个答案中描述的类。
我通过添加其他行来更改构造函数,如下所示:
public MultiPartUtil(String requestURL, String charset) throws IOException {
this.charset = charset;
// creates a unique boundary based on time stamp
boundary = "===" + System.currentTimeMillis() + "===";
URL url = new URL(requestURL);
httpConn = (HttpsURLConnection) url.openConnection();
httpConn.setUseCaches(false);
httpConn.setDoOutput(true); // indicates POST method
httpConn.setDoInput(true);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("Cache-Control", "no-cache");
httpConn.setRequestProperty("enctype", "multipart/form-data");
httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
httpConn.setRequestProperty("User-Agent", "MTApp");
outputStream = httpConn.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, charset), true);
}
上传文件的代码是:
MultiPartUtil multipart = new MultiPartUtil(url, "UTF-8");
multipart.addFilePart("archive", new File("archivefile.zip"));
multipart.finish();
在服务器端,我有:
[HttpPost]
public void upload()
{
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType, "This request is not properly formatted"));
}
//Process file here...
}
在Java代码中我可能会遗漏哪些内容不允许接受该文件?
答案 0 :(得分:0)
你可以试着改变边界来渲染像--- 1234567 ---而不是=== 1234567 ===?
因为http标头参数中不允许使用令牌。