为了账号安全,请及时绑定邮箱和手机立即绑定

服务器跟客户端都报错

服务器:

    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

https://img1.sycdn.imooc.com//5b8411100001290a15940826.jpg


客户端:

    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的文档

https://img1.sycdn.imooc.com//5b84114400018ef216010776.jpg

正在回答

1 回答

原因是上传的图片太大,看了同学们的评论,原来需要在在structs.xml中加一句:

<constant name="struts.multipart.maxSize" value="9000000"/>

9000000是文件大小的限制

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

服务器跟客户端都报错

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信