当前位置: 首页 > 知识库问答 >
问题:

改装-Spring图像上载

何灿
2023-03-14

我有一个问题上传文件到服务器(春季启动)从Android-改版。

这是我在Spring Boot中的代码。

@RestController
@RequestMapping("beongae/api/{version}/profile")
public class ProfileController {
    @RequestMapping(value = "/upload/{name}", method = RequestMethod.POST)
    public ApiMessasge uploadBasic(@PathVariable("name") String name,
                                   @RequestPart("file") MultipartFile data) throws IOException {
        ApiMessasge apiMessasge = new ApiMessasge();
        System.out.println("start upload !!");
        if (!data.isEmpty()) {
            try {
                byte[] bytes = data.getBytes();
                BufferedOutputStream stream = new BufferedOutputStream(
                        new FileOutputStream("./profilepictures/" + new File(name + ".png"))
                );
                stream.write(bytes);
                stream.close();
                apiMessasge.setCode(1);
            } catch (Exception e) {
                System.out.println("Exception : " + e.getMessage());
                for (int i = 0; i < e.getStackTrace().length; i++) {
                    System.out.println(e.getStackTrace()[i].toString());
                }
                apiMessasge.setCode(-1);
            }
        }
        return apiMessasge;
    }
}


这是在Android中

Uri resultUri = result.getUri();
File file = new File(resultUri.getPath());

RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);

MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
@Multipart
@Headers("Content-Type:application/json")
@POST("/beongae/api/{version}/profile/upload/{name}")
Call<ApiMessasge> upload(@Path("version") int version, @Path("name") String fileName
        , @Part MultipartBody.Part file);

这是Spring Boot中的错误消息

但是,当我用邮递员的时候,它做得很好。

请告诉我问题是什么,如何解决

共有1个答案

傅毅然
2023-03-14

试着稍微修改一下你的服务

 @Part("file\"; filename=\"filename.png\" ")
 类似资料:
  • 我必须上传从用户首选的应用程序(Cameta,画廊等)选择后的图像文件。我可以在Imageview中显示结果意图为位图。现在我想上传这个图像后,一个按钮将被点击。我用改装来做到这一点。我跟随ImagePicker类将图像收集到我的Imageview中。 正在收集图像代码: 要将图像上载到服务器,我使用改装库 API接口 我的问题是,在将result intent中的Imageview设置为在服务器

  • 我目前正在做一个个人项目,我想上传一张个人资料图片到我的服务器上。通过以表单数据的形式发送图像,我可以轻松地将图像发送到带有postman的服务器上,但当我对其进行相同的改造时,它不起作用,有时它会创建一个1kb的文件,在打开时显示错误。 如果我提供文件到请求Body.create喜欢, 设置新配置文件pic的代码不会执行,如果我只传递URI。getPath()输入。create()方法id在服务

  • 我正在尝试使用Reform2将图像上载到服务器,但不确定如何执行。 文档让我有点困惑,我尝试了这里提到的解决方案,但它对我不起作用。 这是我当前使用的代码片段,它不会向服务器发送任何内容: 然而,我可以通过使用TypedFile对1.9进行改装来很好地做到这一点。 是我做错了什么还是Retrofit2对这种事情有什么问题?

  • 我正在使用翻新上传图像/文件 在这里,我发布了我的API调用以上传图像 请帮助我如何为这个调用编写接口,我已经用@part和@PartMap试过了,但是不起作用

  • 我是spring boot上的wokring,我已经在这个路径上创建了一个文件夹:myApp/src/web/images 在做的时候 我得到了这个错误: Java语言nio。文件NoSuchFileException:/private/var/folders/4g/wd\u lgz8970sfh64zm38lwfhw0000gn/T/tomcat docbase。8255351399752894

  • 问题内容: 我知道,如果鼠标悬停在文本上方,将允许显示文本。我想知道是否可以在鼠标悬停时更改图像?提前致谢。 问题答案: 我会在您保存图片的组件中添加一个。 然后只需重写方法并 更改图像即可。 这是一个完整的工作示例: