NutzCN Logo
问答 用 C# 代码向 Nutz 上传文件报错。
发布于 200天前 作者 Hanklee0518 1497 次浏览 复制 上一个帖子 下一个帖子
标签:

Java 代码:

    @AdaptBy(type = UploadAdaptor.class)
    @At({"/upload"})
    @Ok("json")
    @POST
    @ApiOperation(name = "文件上传", description = "文件上传")
    @ApiResponses(implementation = Result.class)
    public Result<?> uploadFile(@Param("file") TempFile file,
                                HttpServletRequest request,
                                HttpServletResponse response,
                                AdaptorErrorContext error) throws IOException {
        Dumps.HTTP.all(request);
        if (error != null && error.getAdaptorErr() != null) {
            return Result.error(
                    log.isDebugEnabled() ? error.getAdaptorErr().getMessage() : "文件服务异常"
            );
        }

        String filePath = sysConfigService.getString(SecurityUtil.getAppId(), "UpdateFilesPath");
        String fileName = file.getSubmittedFileName();

        try {
            storageServer.upload(file.getInputStream(), fileName, filePath);
        } catch (IOException e) {
            return Result.error(log.isDebugEnabled() ? e.getMessage() : "文件服务异常");
        }
        return Result.success("文件上传成功");
    }

C# 代码(使用 RestSharp):

        public ReturnResult UploadFile(string appId, string filePath)
        {
            try
            {
                var request = new RestRequest()
                {
                    Method = Method.Post,
                    Resource = $"{baseUri}/upload",
                };
                // 读取文件内容并添加到请求中
                var fileInfo = new FileInfo(filePath);
                using (var fileStream = fileInfo.OpenRead())
                {
                    byte[] buffer = new byte[fileStream.Length];
                    fileStream.Read(buffer, 0, buffer.Length);
                    // 将文件作为字节数组添加到请求中
                    request.AddFile("file", buffer, Path.GetFileName(filePath));
                }

                // 执行请求
                var response = _client.Execute(request);
                // 确保 HTTP 请求成功
                if (response.IsSuccessful)
                {
                    // 读取请求内容
                    string jsonResponse = response.Content;
                    var result = JsonConvert.DeserializeObject<ReturnResult>(jsonResponse);
                    if (result.code != 0)
                    {
                        throw new Exception(result.msg);
                    }
                    else
                    {
                        return result;
                    }
                }
                else
                {
                    throw new Exception($"上传失败,HTTP 状态码:{response.StatusCode}");
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"文件上传出错:{ex.Message}", ex);
            }
        }

Java 服务的日志:

[wk-platform:0.0.0.0:9900] 2024-05-17 09:39:34.265 DEBUG 48152 [] [qtp1515793520-65] org.nutz.mvc.impl.UrlMappingImpl         : Found mapping for [POST] path=/platform/sys/upgrade/upload : AutoUpgradeController.uploadFile(AutoUpgradeController.java:47)
[wk-platform:0.0.0.0:9900] 2024-05-17 09:39:34.265 DEBUG 48152 [] [qtp1515793520-65] org.nutz.ioc.impl.NutIoc                 : Get 'autoUpgradeController'<class com.budwk.app.sys.controllers.sys.AutoUpgradeController>
[wk-platform:0.0.0.0:9900] 2024-05-17 09:39:34.265 DEBUG 48152 [] [qtp1515793520-65] org.nutz.ioc.impl.NutIoc                 : 	 >> Load definition name=autoUpgradeController
[wk-platform:0.0.0.0:9900] 2024-05-17 09:39:34.265 DEBUG 48152 [] [qtp1515793520-65] org.nutz.ioc.impl.NutIoc                 : 	 >> Make...'autoUpgradeController'<class com.budwk.app.sys.controllers.sys.AutoUpgradeController>
[wk-platform:0.0.0.0:9900] 2024-05-17 09:39:34.265 DEBUG 48152 [] [qtp1515793520-65] org.nutz.ioc.impl.ScopeContext           : Save object 'autoUpgradeController' to [app] 
[wk-platform:0.0.0.0:9900] 2024-05-17 09:39:34.265 DEBUG 48152 [] [qtp1515793520-65] o.n.ioc.aop.impl.DefaultMirrorFactory    : Load class com.budwk.app.sys.controllers.sys.AutoUpgradeController without AOP
[wk-platform:0.0.0.0:9900] 2024-05-17 09:39:34.266 DEBUG 48152 [] [qtp1515793520-65] org.nutz.ioc.val.ReferTypeValue          : name=sysConfigService not found, search for type=com.budwk.app.sys.services.SysConfigService
[wk-platform:0.0.0.0:9900] 2024-05-17 09:39:34.266 DEBUG 48152 [] [qtp1515793520-65] org.nutz.ioc.impl.NutIoc                 : Get 'sysConfigServiceImpl'<interface com.budwk.app.sys.services.SysConfigService>
[wk-platform:0.0.0.0:9900] 2024-05-17 09:39:34.267 DEBUG 48152 [] [qtp1515793520-65] org.nutz.ioc.impl.NutIoc                 : Get 'storageServer'<class com.budwk.starter.storage.StorageServer>
[wk-platform:0.0.0.0:9900] 2024-05-17 09:39:34.267 DEBUG 48152 [] [qtp1515793520-65] org.nutz.mvc.upload.UploadAdaptor        : Select Html4 Form upload parser --> /platform/sys/upgrade/upload
[wk-platform:0.0.0.0:9900] 2024-05-17 09:39:34.269 DEBUG 48152 [] [qtp1515793520-65] org.nutz.mvc.upload.FastUploading        : FastUpload : /platform/sys/upgrade/upload
[wk-platform:0.0.0.0:9900] 2024-05-17 09:39:34.269 DEBUG 48152 [] [qtp1515793520-65] org.nutz.mvc.upload.FastUploading        : info created
[wk-platform:0.0.0.0:9900] 2024-05-17 09:39:34.269 DEBUG 48152 [] [qtp1515793520-65] org.nutz.mvc.upload.FastUploading        : Params map created - 0 params
[wk-platform:0.0.0.0:9900] 2024-05-17 09:39:34.269 DEBUG 48152 [] [qtp1515793520-65] org.nutz.mvc.upload.FastUploading        : boundary: 
--"6d6e6370-d29d-452e-bba3-32c1659af770"
[wk-platform:0.0.0.0:9900] 2024-05-17 09:39:34.275 WARN 48152 [] [qtp1515793520-65] org.nutz.mvc.upload.FastUploading        : Fail to find the firstBoundary (--"6d6e6370-d29d-452e-bba3-32c1659af770") in stream, quit!
[wk-platform:0.0.0.0:9900] 2024-05-17 09:39:34.277 DEBUG 48152 [] [qtp1515793520-65] org.nutz.ioc.impl.NutIoc                 : Get 'jedisAgent'<class org.nutz.integration.jedis.JedisAgent>
[wk-platform:0.0.0.0:9900] 2024-05-17 09:39:34.284 DEBUG 48152 [] [qtp1515793520-65] c.b.s.web.processor.WkLogTimeProcessor   : [POST]URI=/platform/sys/upgrade/upload 19ms
[wk-platform:0.0.0.0:9900] 2024-05-17 09:39:34.286 WARN 48152 [] [qtp1515793520-65] c.b.s.web.processor.WkFailProcessor      : Error@/platform/sys/upgrade/upload :

java.lang.NullPointerException: null
	at com.budwk.app.sys.controllers.sys.AutoUpgradeController.uploadFile(AutoUpgradeController.java:55)
	at org.nutz.lang.reflect.f2208e93f382fc933b6f8166ae20a841$FM$uploadFile$7c24870fae435cbba5d65ff07dc25352.invoke(AutoUpgradeController.java)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:31)

1 回复

如果java代码没问题,那就和nutz没关系,c# 另外google吧

添加回复
请先登陆
回到顶部