[英]Spring webflux - file upload doesnt work it throws error when i hit it from postman
[英]Upload with HTTP out of Java doesnt work when file includes “class”
我正在尝试通过 HTTP 从 Eclipse-PlugIn 客户端上传文件。 当我使用 HTTPrequest 的 getOutputStream 添加文件时,包含字符串“class”的文本文件的连接失败。 如果文件中没有“类”,则连接工作正常。 我
不知道为什么会这样。 在服务器端,文件将被上传,链接缩短并可以打开。 一切都按照它应该的方式工作,除了文本包含“类”。
确保使用 form-multipart 编码的 POST,如下例所示:
final String CHARSET = "ISO-8859-1";
final String CRLF = "\r\n";
String formFieldName = "uploadfile";
String fileName = "upload.jpeg";
String fileContentType = "image/jpeg";
URL url = new URL("http://localhost");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true); // if input is expected
String boundary = "--------------------"+System.currentTimeMillis();
StringBuilder postData = new StringBuilder();
postData.append("--").append(boundary).append(CRLF);
postData.append("Content-Disposition: form-data; name=\"").append(formFieldName).append("\"; filename=\"").append(fileName).append("\"").append(CRLF);
postData.append("Content-Type: ").append(fileContentType).append(CRLF);
postData.append(CRLF).append(new String(bytes, CHARSET)).append(CRLF);
postData.append("--").append(boundary).append("--").append(CRLF);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary);
connection.setRequestProperty("Content-Length", Integer.toString(postData.length()));
connection.setFixedLengthStreamingMode(postData.length());
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), CHARSET);
out.write(postData.toString());
out.close();
assert(connection.getResponseCode() == HttpURLConnection.HTTP_OK);
// if input is expected
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), CHARSET));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
干杯,马克斯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.