安卓发送图片文字,java后台接收

安卓使用retrofit2 和rxjava2

url:

  

@Multipart
@POST(UrlTools.STORYUPLOAD)
Observable saveReport(@Part List parts)

activity:

 if (contentText.equals("") && mSelected.size() < 1) {
            ToastUtils.showBottomToast("请输入内容", 0);
            return;
        }
        
        ContentResolver contentResolver = getContentResolver();
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("name", contentText);//表单类型; add需要的文本,第一个参数与后台对应,传输需要的文本
        
        for (int i = 0; i < mSelected.size() - 1; i++) {
            File file1 = null;
            file1 = new File(getFilePathFromContentUri(mSelected.get(i), contentResolver));
            RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file1);
            builder.addFormDataPart("file", file1.getName(), imageBody);//imgfile 后台接收图片流的参数名
        }
        List parts = builder.build().parts();

//        实现请求
        utilsPresenter.getStory(VintageActivity.this, parts);

开始请求:

    //上传文件
    @Override
    public void upFile(BaseImpl context,  List parts) {

        RetrofitRequest.getApiService()
                .saveReport( parts)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver(context, false) {
                    @Override
                    public void onSuccess(Result story) {
                        listener.onSuccess(story);
                    }

                    @Override
                    public void onFail(Result response) {
                        listener.onError(response, STORYLIST);
                    }
                });
    }

 

Java后台 springboot2

  @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public String handleFileUpload(HttpServletRequest request) {
        MultipartHttpServletRequest params = ((MultipartHttpServletRequest) request);
        List files = ((MultipartHttpServletRequest) request)
                .getFiles("file");
        String name = params.getParameter("name");  //参数与安卓addFormDataPart的第一个参数对应
        System.out.println("name:" + name);
        String id = params.getParameter("id");
        System.out.println("id:" + id);

        Map urlMap = new HashMap<>();

        MultipartFile file = null;
        BufferedOutputStream stream = null;
        for (int i = 0; i < files.size(); ++i) {
            file = files.get(i);
            if (!file.isEmpty()) {
                try {
                    byte[] bytes = file.getBytes();
                    stream = new BufferedOutputStream(new FileOutputStream(
                            new File("./../images/"+file.getOriginalFilename())));//存在项目根目录下
                    stream.write(bytes);
                    stream.close();
                    urlMap.put(SERVER_URL+"resource/images/" , file.getOriginalFilename());
                } catch (Exception e) {
                    stream = null;
                    return e.toString();
                }
            } else {
                return "空";
            }
        }
        return “成功”;

 

你可能感兴趣的:(安卓发送图片文字,java后台接收)