阅读:4973回复:0
上传文件时提交参数
接口风格:restful
框架:resteasy 问题:上传文件时,需要提交其他的参数信息. 解决方案:在接口参数中使用pojo类来接受参数,此接口入参为:multipart/form-data.类型 接口参数中的的pojo类,使用@MultipartForm来标注. 此个pojo类中,封装好接口中所需要的所有参数.包含二进制文件. 源代码如下:(因涉业务,此代码只展示片段代码.) controller层代码: @POST @Path(value = "audit") @Consumes("multipart/form-data") public Response codeAudit(@MultipartForm ApiProject diskFile, @Context HttpServletRequest request) { System.out.println(diskFile); return Response.status(200).entity(JSONObject.quote("上传文件名: ")).build(); } pojo类: public class ApiProject { String fileName; String projectKey; String projectVersion; String projectLanguage; String projectEncoding; String projectSources; String projectClassPath; String jobKey; private byte[] fileDate; public String getFileName() { return fileName; } @FormParam("fileName") public void setFileName(String fileName) { this.fileName = fileName; } public byte[] getFileDate() { return fileDate; } @FormParam("file_upload")//此处名字,应与前台上传的文件name相等. @PartType(MediaType.APPLICATION_OCTET_STREAM)//上传类型. public void setFileDate(byte[] fileDate) { this.fileDate = fileDate; } public String getProjectKey() { return projectKey; } @FormParam("projectKey") public void setProjectKey(String projectKey) { this.projectKey = projectKey; } public String getProjectVersion() { return projectVersion; } @FormParam("projectVersion") public void setProjectVersion(String projectVersion) { this.projectVersion = projectVersion; } public String getProjectLanguage() { return projectLanguage; } @FormParam("projectLanguage") public void setProjectLanguage(String projectLanguage) { this.projectLanguage = projectLanguage; } public String getProjectEncoding() { return projectEncoding; } @FormParam("projectEncoding") public void setProjectEncoding(String projectEncoding) { this.projectEncoding = projectEncoding; } public String getProjectSources() { return projectSources; } @FormParam("projectSources") public void setProjectSources(String projectSources) { this.projectSources = projectSources; } public String getProjectClassPath() { return projectClassPath; } @FormParam("projectClassPath") public void setProjectClassPath(String projectClassPath) { this.projectClassPath = projectClassPath; } public String getJobKey() { return jobKey; } @FormParam("jobKey") public void setJobKey(String jobKey) { this.jobKey = jobKey; } } |
|