@ServiceMethod(method = "common.uploadFile", version = "1.0", needInSession = NeedInSessionType.NO, httpAction = HttpAction.POST) public Object uploadFile(UploadFileRequest request){ try { String fileType = request.getFile().getFileType(); int length = request.getFile().getContent().length; ClassPathResource outFile = new ClassPathResource("/"); String path = outFile.getFile().getParent() + "/1." + fileType; FileCopyUtils.copy(request.getFile().getContent(), new File(path)); UploadFileResponse response = new UploadFileResponse(); response.setFileType(fileType); response.setLength(length); response.setPath(path); return response; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
public static void main(String[] args) throws IOException { // TODO Auto-generated method stub //http://localhost:8080/jojowo/api?method=user.logon&v=1.0&messageFormat=json&appKey=00001&loginName=admin&password=admin RestTemplate restTemplate = new RestTemplate(); MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>(); form.add("method", "common.uploadFile"); form.add("appKey", "00001"); form.add("v", "1.0"); form.add("messageFormat", "json"); form.add("id", "1"); /*String sign = RopUtils.sign(form.toSingleValueMap(), "abcdeabcdeabcdeabcdeabcde"); form.add("sign", sign); */ ClassPathResource resource = new ClassPathResource("test.txt"); UploadFile uploadFile = new UploadFile(resource.getFile()); //②添加一个上传的文件,photo参数不参与签名 form.add("file", "txt@" + Base64.encodeBase64String(uploadFile.getContent())); String SERVER_URL = "http://localhost:8080/jojowo/api"; //③调用上传文件服务 String response = restTemplate.postForObject(SERVER_URL, form, String.class); System.out.println("response:\n" + response); }