疯狂的食堂老大妈 2015-05-26 01:32 采纳率: 0%
浏览 1798

EXT上传图片,怎么获取图片对象?

我用ext做上传图片,图片要存到数据库中去,我本想的是获取地址,然后将图片对象转为二进制存如数据库,但是路径一直有问题,然后我自己写了一个死路径,图片就存到数据库了,求java解后台怎么获取对象,网上说的那些设置html中。的type那个对我这个没用

 {                                                                                                  
    xtype: 'fieldset',                                                                      
    autoHeight:true,
    title: '签名图片',
    labelWidth  : 80,                                                                               
    layout      : 'form',                                                                           
    items:[{                                                                                                
        xtype: 'textfield',                                                                 
            id: 'userInfoForm_UserForm_fileImg',                                    
            name: 'fileImg',                                                                    
            width:355,  
            height:20,                                                                          
            fieldLabel  : '签名图片',               
            inputType : 'file',     
            fileUpload: true ,  
            frame: true,                                    
            maxLength : 50                                                                          
            }],                                                                                             
        buttons: [{                                                                                             
            text:'上传签名',                            
            handler:function () {                                                                   
                var UserFilePath = Ext.getCmp('userInfoForm_UserForm_fileImg').getValue();  var UserFilePaths='';
                var UserName=Ext.getCmp('userInfoForm_UserForm_username').getValue(); 
                if(UserFilePath!=null&&UserFilePath!=''){       
                    UserFilePaths=UserFilePath.substring(UserFilePath.length-3,UserFilePath.length);    
                    if(UserFilePaths=='jpg'||UserFilePaths=='gif'||UserFilePaths=='png'||UserFilePaths=='bmp'||UserFilePaths=='peg'){//判断是否是图片
                        Ext.getCmp('userInfoForm_UserForm').form.doAction('submit',{        //提交            
                            url:'userInfo.html?method=saves',   
                            params:{UserFilePath:UserFilePath,UserName:UserName},   
                            callback:function(options,success,response){
                            var json = Ext.util.JSON.decode(response.responseText);     
                            if(json.success){
                                        showMsg('签名图片上传成功!','3');   
                                        }else{alert('上传失败');}   
                                    }
                                }); 
                            }else{showMsg('请选择正确的图片!','3');}        
                        }else{showMsg('请选择图片再点击上传!','3');}          
                }                   
            }
            ]},

上面的前台代码是在java类中写的,用的stringbuff,然后输出到前台显示的

 public void saves(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
        String UserFilePath=(String)request.getAttribute("UserFilePath") ;//获取前台路径(但是传入的是C:\fakepath\文件名称)
        String UserName=request.getParameter("UserName"); //用户账号 
        System.out.println(UserFilePath);
//      String path =request.getRealPath("/files");     
        JSONObject json = new JSONObject();
        User user=new User();
        boolean flag=false;
        try {
            File files=new File(UserFilePath);
            byte[] destFileByte= FileUtils.readFileToByteArray(files) ;//转换为二进制
            IUserManager userManager = (IUserManager) AppService.System.UserManager.get(this);//创建用户管理对象
            user.setSigmature_Img(destFileByte);//将图片存入实体类
            user.setUsername(UserName);//姓名
            flag=userManager.InsertSigmature_Img(user);//成功返回true
            if(flag){
                json.put("success",true);
            }
            json.put("success","111");
        } catch (Exception e) {
            e.printStackTrace();
        }
        writeJson(response,json);
    }
  • 写回答

3条回答 默认 最新

  • O溺水的鱼0 2015-05-26 02:38
    关注

    获取图片的时候,服务器上把存储的图片流信息读取出来,返回给前端。在http协议中,content-type上设置返回的图片格式,如jpg、png等,前端自动就会显示成图片了。

    评论
  • Go 旅城通票 2015-05-26 02:52
    关注

    安全问题客户端路径是不会回发的。而且楼主连客户端和服务器端都分不清楚。就算能获取到客户端路径,你的代码是在服务器端运行,这个路径在服务器端不一定存在,存在对应的文件也不一定有。。你在你电脑测试当然可以,但是发布到服务上文件路径就完全不一样了,此时是服务器端的路径

    自己看这个怎么获取提交的文件,而不是通过客户端路径来获取:
    http://blog.csdn.net/chuyuqing/article/details/8635732
    http://www.cnblogs.com/xdp-gacl/p/4200090.html

    评论
  • 邹邹wl 2015-05-26 23:50
    关注

    你可以搞个文件服务器,数据库保存地址,指向文件服务起的图片位置就行了。

    评论

报告相同问题?

悬赏问题

  • ¥15 eclipse无法正常运行
  • ¥15 定义了函数,但是无法根据函数定义触发器
  • ¥20 5变量卡诺图化简得出与非门电路图
  • ¥15 Python爬取交通拥堵指数数据
  • ¥15 使用vba抓取重定向网页问题
  • ¥20 付费需求测试程序(细谈)。
  • ¥15 为什么这段c++代码会报这么多语法错误?
  • ¥20 如何利用C语言实现用最小二乘法选配两个经验公式
  • ¥50 vue-codemirror如何对指定行 指定位置的 字符进行背景颜色或者字体颜色的修改?
  • ¥30 遇到一个的问题,请教各位