NutzCN Logo
精华 swagger 生成上传文件接口怎么处理
发布于 2035天前 作者 JsckChin 6803 次浏览 复制 上一个帖子 下一个帖子
标签:
@ApiOperation(value = "上传图片", notes = "上传图片", httpMethod="POST" ,consumes="application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Filedata", paramType="query", value = "临时文件", dataType="file", required = true),
    })
	@AdaptBy(type = UploadAdaptor.class, args = {"ioc:imageUpload"})
    @POST
    @At
    @Ok("json")
    //AdaptorErrorContext必须是最后一个参数
    public Object image(@Param("Filedata") TempFile tf, HttpServletRequest req, AdaptorErrorContext err) {
        try {
            if (err != null && err.getAdaptorErr() != null) {
                return Result.error(1,"upload.notvalid");
            } else if (tf == null) {
                return Result.error("upload.empty");
            } else {
                String p = Globals.AppRoot;
                String f = Globals.AppUploadPath + "/image/" + DateUtil.format(new Date(), "yyyyMMdd") + "/" + R.UU32() + tf.getSubmittedFileName().substring(tf.getSubmittedFileName().indexOf("."));
                Files.write(new File(p + f), tf.getInputStream());
                return Result.success("upload.success", Globals.AppBase+f);
            }
        } catch (Exception e) {
            return Result.error();
        } catch (Throwable e) {
            return Result.error("upload.formaterror");
        }
    }
4 回复

consume写mulitpart试试

还是不行 返回结果如下

 cache-control: no-cache 
 content-type: application/json;charset=utf-8 
 date: Tue, 28 May 2019 11:31:10 GMT 
 server: Jetty(9.4.3.v20170317) 
 transfer-encoding: chunked 
 x-powered-by: nutz/1.r.68-SNAPSHOT <nutzam.com> 
{
  "code": 1,
  "msg": "文件不合法"
}

不晓得了。。。 查查swagger的文档吧

问题已解决。
重点是 consumes="multipart/form-data" paramType="form" 两个设置

@ApiOperation(value = "上传图片", notes = "上传图片", httpMethod="POST" ,consumes="multipart/form-data")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Filedata", paramType="form", value = "临时文件", dataType="file", required = true),
    })
添加回复
请先登陆
回到顶部