当我通过Multipart库在服务器上上传文件时,Multipart向我显示文件上传成功,但是当我看到服务器中的文件时服务器上没有文件。
我正在使用以下代码。请在缺少某些代码或其他任何内容的地方进行检查
String API_URL_POST_COMPLAINT
="";
String responseResult = "0";
try {
SharedPreferences sharedPreferences =
getApplicationContext().getSharedPreferences("lusername", Context.MODE_PRIVATE);
String userId = sharedPreferences.getString("username", "");
Uri uri = Uri.parse(result.getUri().toString());
String realPath = ImageFilePath.getPath(MainActivity.this, uri);
File picturePathFile = new File(realPath);
String complaint="Hello";
String apiUrl = API_URL_POST_COMPLAINT + "Token=" + "isuotb4hmtXrOcB0IhHdkDbL7NReoacMpYDHWRUFtWYj7l-Ps2Y=" + "&WOID="+URLEncoder.encode("52326", "UTF-8") +"&DocTitle="+URLEncoder.encode("Hi", "UTF-8")+"&FileName="+picturePathFile;
apiUrl.replaceAll(" ", "%20");
apiUrl.replaceAll("\n", "%20");
MyMultipartRequest multipart = new MyMultipartRequest(getApplicationContext(), apiUrl);
multipart.addHeaderField("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
multipart.addHeaderField("Test-Header", "This is my test Header field");
multipart.addFormField("Token", "isuotb4hmtXrOcB0IhHdkDbL7NReoacMpYDHWRUFtWYj7l-Ps2Y=");
multipart.addFormField("WOID", "52326");
multipart.addFormField("DocTitle", "Hello");
multipart.addFilePart("FileName", picturePathFile);
System.out.println("complaintFile = " + complaint);
System.out.println("picturePathFile = " + picturePathFile);
System.out.println("complaintFile = " + picturePathFile.getName());
String responsePost = multipart.finish();
System.out.println("responsePost = " + responsePost);
return responsePost;
} catch (Exception e) {
e.printStackTrace();
responseResult = "0";
}
return responseResult;
.Net代码在这里
[HttpPost]
public Result UploadDoc(string Token, int WOID, string DocTitle, string
FileName)
{
if (!Request.Content.IsMimeMultipartContent() ||
HttpContext.Current.Request.Files.Count == 0)
return new Result(Result.ResultCode.NoAction, "File not found");
byte[] lBytes = new byte[HttpContext.Current.Request.Files(0).ContentLength -
1 + 1];
HttpContext.Current.Request.Files(0).InputStream.Read(lBytes, 0,
HttpContext.Current.Request.Files(0).ContentLength - 1);
Result lRes = UserClass.GetUserFromToken(Token);
if (lRes.NoSuccess)
return lRes;
UserClass lUser = (UserClass)lRes.RetInfo;
if (lUser.RoleType == UserClass.UserRoleType.Read_Only)
return lRes.Init(Result.ResultCode.NoAction, "Permission Denied for User
Role");
if (WorkOrderClass.SearchByUser(WOID.ToString(), lUser, true).Length == 0)
lRes.Init(Result.ResultCode.NoAction, "Permission Denied on WO");
return (new DocumentClass() { WorkOrderID = WOID, UploadedBy = lUser.UserID,
DocTitle = DocTitle.Trim(), FileName = FileName, Doc = lBytes
}).InsertIntoDB;
}
答案 0 :(得分:3)
您必须添加文件类型(您要上传的文件类型)。并尝试使用okhttp上传。
private ProgressDialog pDialog;
private class FileAsync extends AsyncTask<String, String, String> {
@SuppressLint("ResourceType")
@Override
protected String doInBackground(String... params) {
String result = "";
try {
OkHttpClient okHttpClient = getOkHttpClient();
MultipartBody.Builder data = new MultipartBody.Builder();
data.setType(MultipartBody.FORM);
MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
data.addFormDataPart("FileName","file.png",
RequestBody.create(MEDIA_TYPE_PNG, picturePathFile));
RequestBody requestBody = data.build();
Request request = new Request.Builder()
.url("Your URL").post(requestBody)
.build();
Response response = okHttpClient.newCall(request).execute();
result = response.body().string().toString();
Log.e("Result : ", result);
} catch (IOException ex) {
System.err.println(ex);
}
return result;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(activity);
pDialog.setMessage("Please wait...");
pDialog.show();
pDialog.setCanceledOnTouchOutside(false);
}
@Override
protected void onPostExecute(String response) {
super.onPostExecute(response);
if (pDialog != null && pDialog.isShowing()) {
pDialog.dismiss();
}
if (response != null) {
//Do something with your response.
}
}
}
public OkHttpClient getOkHttpClient() {
return new OkHttpClient.Builder()
.connectTimeout(120, TimeUnit.SECONDS)
.writeTimeout(120, TimeUnit.SECONDS)
.readTimeout(120, TimeUnit.SECONDS)
.retryOnConnectionFailure(true)
.build();
}