在node js服务器android上上载图像(多部分数据)

在node js服务器android上上载图像(多部分数据),android,node.js,retrofit2,image-uploading,okhttp3,Android,Node.js,Retrofit2,Image Uploading,Okhttp3,我试图通过android将图像上传到我的nodejs服务器上,但在android中失败了。我正在使用okhttp和改装库。 我已经搜索了许多关于堆栈溢出和一些博客的解决方案,但在我的案例中没有一个是有效的 我的节点js上传代码: const express = require('express'); const bodyParser = require('body-parser'); const authenticate = require('../authenticate'); const m

我试图通过android将图像上传到我的nodejs服务器上,但在android中失败了。我正在使用okhttp和改装库。 我已经搜索了许多关于堆栈溢出和一些博客的解决方案,但在我的案例中没有一个是有效的

我的节点js上传代码:

const express = require('express');
const bodyParser = require('body-parser');
const authenticate = require('../authenticate');
const multer = require('multer');
const cors = require('./cors');
const crypto = require("crypto");



const storage = multer.diskStorage({
    destination:(req,file,cb) => {
        cb(null,'/mnt/images');
    },

    filename : (req,file,cb) => {
        cb(null,/*file.originalname*/crypto.randomBytes(16).toString("hex"))
    }
});

const imageFileFilter = (req,file,cb) => {

    if(!file.originalname.match(/\.(jpg|jpeg|png)$/)){
        return cb(new Error('You can upload image files!'),false);
    }else{
        cb(null,true);
    }



};

const upload = multer({storage : storage,fileFilter:imageFileFilter});


const uploadRouter = express.Router();

uploadRouter.use(bodyParser.json());

uploadRouter.route('/')
.options(cors.corsWithOptions, (req, res) => { res.sendStatus(200) })
.get(cors.cors,authenticate.verifyUser, authenticate.verifyAdmin, (req, res, next) => {
    res.statusCode = 403;
    res.end('PUT operation not supported on /imageUpload');
})
.get(cors.corsWithOptions,authenticate.verifyUser,authenticate.verifyUser, authenticate.verifyAdmin, (req, res, next) => {
    res.statusCode = 403;
    res.end('GET operation not supported on /imageUpload');
})
.delete(cors.corsWithOptions,authenticate.verifyUser,authenticate.verifyUser, authenticate.verifyAdmin, (req, res, next) => {
    res.statusCode = 403;
    res.end('DELTE operation not supported on /imageUpload');
})
.post(cors.cors,authenticate.verifyUser,
    upload.single('imageFile'), (req, res) => {
        res.statusCode = 200;
        res.setHeader('Content-Type','application/json');
        res.json(req.file);   
});


module.exports = uploadRouter;
我想上传图像的关键和图像的价值,如图所示与邮政。它的作品很好的邮递员

我的Android代码: 上传功能:

private void upload(){

        ServiceGenerator serviceGenerator = new ServiceGenerator();
        ApiInterface apiInterface = serviceGenerator.createImageUploadService(ApiInterface.class,getContext());
        File file = new File(filePath.getPath());
        RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("imageFile", file.getName(), reqFile);
        RequestBody name = RequestBody.create(MediaType.parse("text/plain"), "imageFile");
        Call<ResponseBody> call = apiInterface.postImage("bearer "+sharedPreferences.getString(Constants.JWT,""),body,name);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Log.i("RESPONSE","1");
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i("RESPONSE","0");
                Log.i("RESPONSE",t.getCause().toString());

            }
        });

    }
private void upload(){
ServiceGenerator ServiceGenerator=新ServiceGenerator();
ApiInterface ApiInterface=serviceGenerator.createImageUploadService(ApiInterface.class,getContext());
File File=新文件(filePath.getPath());
RequestBody reqFile=RequestBody.create(MediaType.parse(“image/*”),文件);
MultipartBody.Part body=MultipartBody.Part.createFormData(“imageFile”,file.getName(),reqFile);
RequestBody name=RequestBody.create(MediaType.parse(“text/plain”),“imageFile”);
Call Call=apinterface.posimage(“bearer”+SharedReferences.getString(Constants.JWT,”),body,name);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
日志i(“响应”、“1”);
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.i(“响应”,“0”);
Log.i(“RESPONSE”,t.getCause().toString());
}
});
}
服务生成器CreateUploadService函数:

public <T> T createImageUploadService(Class<T> serviceClass, final Context context){

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();

        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();


        if(retrofit == null){
            retrofit = new Retrofit.Builder()
                    .client(client)
                    .baseUrl(Constants.BASE_API_URL)
                    /*.addConverterFactory(GsonConverterFactory.create(gson))*/
                    .build();
        }
        return retrofit.create(serviceClass);
    }
public T createImageUploadService(类serviceClass,最终上下文){
HttpLoggingInterceptor拦截器=新的HttpLoggingInterceptor();
拦截器.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient客户端=新建OkHttpClient.Builder().addInterceptor(拦截器).build();
如果(改装==null){
改装=新改装.Builder()
.客户(客户)
.baseUrl(常量.BASE\u API\u URL)
/*.addConverterFactory(GsonConverterFactory.create(gson))*/
.build();
}
返回改装。创建(serviceClass);
}
API接口:

@Multipart
    @POST("imageUpload")
    Call<ResponseBody> postImage(@Header("Authorization") String authHeader,@Part MultipartBody.Part image, @Part("name") RequestBody name);
@Multipart
@POST(“图像上传”)
调用postImage(@Header(“Authorization”)字符串authHeader、@Part MultipartBody.Part image、@Part(“name”)RequestBody name);

你能发布你的android代码吗?如果你有任何错误,我已经更新了我的帖子,请检查@kdblue。如果你有任何错误,请检查你的日志。不,它没有提供任何休息,它既没有失败,也没有成功@kdblue你尝试过调试吗?