0
回答
基于jersey的REST API的客户端程序开发,如何根据URI传文件请求

/service/applications/{applicationName}/timeout/{timeout}

HTTP Methods

POST

Deploys an application to the service grid. An application is consisted of a group of services that might have dependencies between themselves. The application will be deployed according to the dependency order defined in the application file and deployed asynchronously if possible.

Parameters

Type Name Description Mandatory Parameter type Default value
String applicationName The application name true PathVariable
int timeout true PathVariable
MultipartFile file The compressed application file true RequestParam
String authGroups The authorization groups for which this deployment will be available false RequestParam
MultipartFile recipeOverridesFile The application overrides file - to overrides the application properties false RequestParam
MultipartFile cloudOverridesFile File of overriding cloud properties false RequestParam
Boolean selfHealing if true, there will be an attempt to restart the recipe in case a problem occurred in its life-cycle, otherwise, if the recipe fails to execute, no attempt to recover will made false RequestParam
boolean debugAll Indicates if all lifecycle events should have a breakpoint false RequestParam false
String debugEvents Indicates which lifecycle events should have a breakpoint false RequestParam
String debugMode Indicates the debug mode to use One of 'instead', 'after' or 'onError' false RequestParam

Request

Request body for a POST on /service/applications/{applicationName}/timeout/{timeout}
{
  "applicationName" : "petclinic",
  "srcFile" : "packaged application file",
  "recipeOverridesFile" : "recipe overrides file"
}

Response

Response to a POST on /service/applications/{applicationName}/timeout/{timeout}
{
  "status" : "success",
  "response" : {
    "serviceOrder" : "[mongod,mongoConfig,apacheLB,mongos,tomcat]",
    "lifecycleEventContainerID" : "07db2a16-62f8-4669-ac41-ed9afe3a3b02"
  }
}
  • Returns "200 OK" - success.
  • Returns "500 Internal Server Error" - DSLException.
  • Returns "500 Internal Server Error" - IOException.

下面是客户端代码:

public class FileUploadController extends SimpleFormController {
    
    protected ModelAndView onSubmit(
        HttpServletRequest request,
        HttpServletResponse response,
        Object command,
        BindException errors){
        
        try
        {
            // cast the bean
            FileUploadBean bean = (FileUploadBean) command;

            // let's see if there's content there
            MultipartFile file = bean.getFile();
                               
            if (file == null) {
                throw new Exception("上传失败:文件为空");    
            }
            if(file.getSize()>10000000)        
            {
                throw new Exception("上传失败:文件大小不能超过10M");            
            }
            //得到文件名
//            String filename=file.getOriginalFilename();        
            
            if(file.getSize()>0){   
            	Client client = Client.create();
            	try{
            		URI u = new URI("http://10.10.4.128:8100/service/applications/helloworld/timeout/20");
            		WebResource resource = client.resource(u);
            		StringKeyObjectValueIgnoreCaseMultivaluedMap params = new StringKeyObjectValueIgnoreCaseMultivaluedMap();
                	params.add("file", (Object)file);
                	String result = resource.queryParam(params).type(MediaType.MULTIPART_FORM_DATA).post(String.class); //queryParam只能传key-value都是string类型
                	System.out.println(result);
            	}catch (Exception e) {
					System.out.println(e.toString());
				}     
            }
            else{
                throw new Exception("上传失败:上传文件不能为空");
            }
            // well, let's do nothing with the bean for now and return:
            try {
                return super.onSubmit(request, response, command, errors);
                
            } catch (Exception e) {
                System.out.println(e.getMessage());
                return null;
            }
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
            return null;
        }
    }   
}

异常是肯定的,将file对象转为string并以“?”的方式请求

com.sun.jersey.api.client.UniformInterfaceException: POST http://10.10.4.128:8100/service/applications/helloworld/timeout/20?file=org.springframework.web.multipart.commons.CommonsMultipartFile@efe2dd returned a response status of 500 Internal Server Error

举报
chape
发帖于5年前 0回/4K+阅

以下是问题补充:

  • @chape :对jersey不熟悉,查了下jersey的API还是没找到,不知道如何传对象参数,希望知道的指点下,谢谢了 (5年前)
顶部