我在android中创建了一个用于上传文件的程序。一切都很完美。但是,当我上传一个超过1Mb的文件时,我从我的服务器收到此错误:“上传时出错”
这是我的UploadFile()方法
private void uploadFile() {
dialog = ProgressDialog.show(getActivity(), "", "Uploading File...", true);
// Map is used to multipart the file using okhttp3.RequestBody
Map<String, RequestBody> map = new HashMap<>();
long maxLength = 10000000;
File file = new File(selectedFilePath);
if(file.length() > maxLength){
Toast.makeText(getActivity(), "can't upload file if size more than 10mb", Toast.LENGTH_LONG).show();
dialog.dismiss();
}else {
String name = tv_name.getText().toString();
String email = tv_email.getText().toString();
// Parsing any Media type file
RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file);
RequestBody requestBody1 = RequestBody.create(MediaType.parse("text/plain"), name);
RequestBody requestBody2 = RequestBody.create(MediaType.parse("text/plain"), email);
map.put("file\"; filename=\"" + selectedFilePath + "\"", requestBody);
map.put("name\"; username=\"" + name + "\"", requestBody1);
map.put("email\"; email=\"" + email + "\"", requestBody2);
ApiConfig getResponse = AppConfig.getRetrofit().create(ApiConfig.class);
Call<ServerResponse> call = getResponse.upload("token", map);
call.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
if(dialog.isShowing()){
dialog.dismiss();
}
ServerResponse serverResponse = response.body();
if (serverResponse != null) {
if (serverResponse.getSuccess()) {
Toast.makeText(getActivity(), serverResponse.getMessage(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), serverResponse.getMessage(), Toast.LENGTH_SHORT).show();
}
} else {
// Log.v("Response", serverResponse.toString());
}
// dialog.dismiss();
goToProfile();
}
@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
t.printStackTrace();
dialog.dismiss();
Toast.makeText(getActivity(), "Failed, Please Try Again", Toast.LENGTH_SHORT).show();
}
});
}
}
超时
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.MINUTES)
.writeTimeout(5, TimeUnit.MINUTES)
.readTimeout(5, TimeUnit.MINUTES).build();
return new Retrofit.Builder()
.baseUrl(AppConfig.BASE_URL).client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
这是我的PHP代码
$target_dir = "uploads/";
$target_file_name = $target_dir .basename($_FILES["file"]["name"]);
$response = array();
// Check if image file is a actual image or fake image
if (isset($_FILES["file"])) {
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file_name)) {
$success = true;
$message = "Successfully Uploaded";
} else {
$success = false;
$message = "Error while uploading";
}
} else {
$success = false;
$message = "Required Field Missing";
}
$response["success"] = $success;
$response["message"] = $message;
echo json_encode($response);
?>
答案 0 :(得分:0)
尝试压缩文件并重新上传。
答案 1 :(得分:0)
使用此改装配置:
public static OkHttpClient okHttpClient1 = new OkHttpClient().newBuilder()
.connectTimeout(150, TimeUnit.SECONDS)
.writeTimeout(150, TimeUnit.SECONDS)
.readTimeout(150, TimeUnit.SECONDS)
.build();
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(AppConfig.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient1)
.build();
}
return retrofit;
}
简而言之,我的意思是说,增加你在Android端的超时。