我用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);
}
获取图片的时候,服务器上把存储的图片流信息读取出来,返回给前端。在http协议中,content-type上设置返回的图片格式,如jpg、png等,前端自动就会显示成图片了。
安全问题客户端路径是不会回发的。而且楼主连客户端和服务器端都分不清楚。就算能获取到客户端路径,你的代码是在服务器端运行,这个路径在服务器端不一定存在,存在对应的文件也不一定有。。你在你电脑测试当然可以,但是发布到服务上文件路径就完全不一样了,此时是服务器端的路径
自己看这个怎么获取提交的文件,而不是通过客户端路径来获取:
http://blog.csdn.net/chuyuqing/article/details/8635732
http://www.cnblogs.com/xdp-gacl/p/4200090.html
你可以搞个文件服务器,数据库保存地址,指向文件服务起的图片位置就行了。