【发布时间】:2016-09-25 12:56:25
【问题描述】:
我想将文件上传到我的网络服务器。 我的服务器实现了 RESTful API。 要上传文件,我应该传递 4 个参数:文件、文件名、项目、版本
我想从控制台上传带有这 3 个参数的文件。我试过这个
URL obj = new URL(MY_URL);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(con.getOutputStream());
ToSend send = new ToSend();
send.file = Files.readAllBytes(Paths.get("path"));
send.file_name = "name";
send.project = "test";
send.version = "1";
objectOutputStream.writeObject(send);
objectOutputStream.flush();
objectOutputStream.close();
我发送的对象是这样的
class ToSend implements Serializable {
byte[] file;
String file_name;
String project;
String version;
}
我收到Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL:
如果这很重要,我用 Java+Spring 编写的服务器和用于此的方法具有下一个签名
@RequestMapping(value = "/", method = RequestMethod.POST)
public @ResponseBody
Response<Boolean> upload(
@RequestParam("project") String project,
@RequestParam("version") String version,
@RequestParam("file_name") String fileName,
@RequestParam("file") MultipartFile file
) throws ServiceException, BadFileExtension {
boolean success = storageService.uploadFile(file, project, fileName, version);
return builder.get(success);
}
【问题讨论】:
标签: java objectoutputstream sendfile