服務器跟客戶端都報錯
服務器:
??? public String uploadInfo() throws IOException{
?? ??? ?
?? ??? ?System.out.println(username + "," +password);
?? ??? ?
?? ??? ?if(mPhoto == null){
?? ??? ??? ?System.out.println(mPhotoFileName + "is null .");
?? ??? ?}
?? ??? ?String dir = ServletActionContext.getServletContext().getRealPath("files");
?? ??? ?
?? ??? ?File file = new File(dir,mPhotoFileName);
?? ??? ?
?? ??? ?FileUtils.copyFile(mPhoto,file);//上傳文件
?? ??? ?
?? ??? ?return null;
?? ?}
ERROR org.apache.struts2.dispatcher.Dispatcher - Could not find action or result
/imooc_okhttp/uploadInfo
客戶端:
????public?void?doUpload(View?view)?throws?IOException?{ ???? ????????File?file?=?new?File(Environment.getExternalStorageDirectory(),"temp.jpg"); ????????if?(!file.exists()){ ????????????L.e(file.getAbsolutePath()+"not?exist!"); ????????????return; ????????} ?????????RequestBody?requestBody?=?new?MultipartBody.Builder() ????????????????.setType(MultipartBody.FORM) ????????????????.addFormDataPart("username","hyman") ????????????????.addFormDataPart("password","1234") ????????????????.addFormDataPart("mPhoto","temp.jpg",RequestBody.create(MediaType.parse("application/octet-stream"),file)) ????????????????.build(); ????????Request?request?=?new?Request.Builder() ????????????????.url(BaseUrl+"uploadInfo") ????????????????.post(requestBody) ????????????????.build(); ???????????????? ????????executeRequest(request); ????}
點擊按鈕之后是html的文檔
2018-08-27
原因是上傳的圖片太大,看了同學們的評論,原來需要在在structs.xml中加一句:
<constant name="struts.multipart.maxSize" value="9000000"/>
9000000是文件大小的限制