2016-05-27 9 views
1

ファイルを自分のWebサーバーにアップロードします。 私のサーバーはRESTful APIを実装しています。 ファイルをアップロードするには、ファイル、ファイル名、プロジェクト、バージョンの4つのパラメータを渡す必要があります他のデータと一緒にコンソールからファイルをアップロードするには

コンソールからこの3つのパラメータでファイルをアップロードします。私はこれが重要な場合は、このためのJava +春と方法で書かれた私のサーバーは、次の署名を持っているException in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL:

を取得し、この

class ToSend implements Serializable { 
    byte[] file; 

    String file_name; 

    String project; 

    String version; 
} 

のように見える私はこの

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(); 

と私が送ったオブジェクトを試してみました

@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); 
} 

答えて