1. 程式人生 > >安卓發送圖片文字,java後臺接收

安卓發送圖片文字,java後臺接收

name ucc selected ise formdata reat tty resp write

安卓使用retrofit2 和rxjava2

url:

  

@Multipart
@POST(UrlTools.STORYUPLOAD)
Observable<Result> saveReport(@Part List<MultipartBody.Part> 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<MultipartBody.Part> parts = builder.build().parts(); // 實現請求 utilsPresenter.getStory(VintageActivity.this, parts);

開始請求:

    //上傳文件
    @Override
    public void upFile(BaseImpl context,  List<MultipartBody.Part> parts) {

        RetrofitRequest.getApiService()
                .saveReport( parts)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<Result>(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<MultipartFile> 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<String, String> 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後臺接收