我正在尝试通过android将其工作文件在邮递员上上传到我的nodejs服务器上的图像,但在android中失败。我正在使用okhttp和改造库。 我已经搜索了很多关于堆栈溢出的解决方案和一些博客,但是在我的情况下它们都没有起作用。Retrofit调用什么都不会返回。
我的节点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());
}
});
}
服务生成器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);
}
API接口:
@Multipart
@POST("imageUpload")
Call<ResponseBody> postImage(@Header("Authorization") String authHeader,@Part MultipartBody.Part image, @Part("name") RequestBody name);