jquery  ajaxFileUpload  上传文件方法 , 局部上传

js  定义集合变量  var  a={a:方法,b,方法};

var Util = {
autoTjSqUpload : function(fileid) {
$.ajaxFileUpload({
url : 'apply/memberupload!uploadshenqing',
secureuri : false,
fileElementId : fileid,
dataType : 'json',
success : function(data) {
if (data.error) {
$('#uploadbtn1').val("重传");
$('#status1').val("0");
alert(data.error);
} else {
$('#uploadbtn1').val("重传");
$('#status1').val("1");
$('#chenggongdiv1').html("上传成功");
}
}
});
},

文件上传    

1.文件上传验证
页面:
<tr>
<td style="padding-left:20px; font-weight:bold;">机构有效身份证明文件<div style="color:#F00; font-weight:100; padding-top:4px;">*图片格式JPG或BMP<br />大小不超过300K</div></td>
<td style="150px"><input type="file" name="m_shenfen" id="m_shenfen" /></td>
<td>
<input type="button" onclick="Util.autoJgzcUpload('m_shenfen')" id="uploadbtn1" value="上传" /><span id="chenggongdiv1" style="70px;font:12px;margin-left:15px;color:red;visibility:hidden">上传成功</span>
<input type="hidden" name="status" id="status1" value="${status[0]}"/>
</td>
</tr>
action 执行:
if(!hash.containsKey("shenfenzheng"))
{
addFieldError("errormsg","机构有效身份证明文件 必须上传!");

return false;
}
判断hashMap是否存在shenfenzheng;
action判断验证:
private File m_shenfen;//机构有效身份证明文件
private String m_shenfenContentType;// 上传文件类型
private String m_shenfenFileName;// 上传头像文件名称
private String ext;// 保存文件后缀
private String message;
private boolean verification=true;
//ajax 输出流-------------------------------------------------------------------------
private InputStream textmessage;

//上传机构有效身份证明文件
public String shenFenupload(){
try {
//验证信息有效性
this.verifyJpg(m_shenfenFileName,m_shenfen);

if(this.verification){
//保存文件
String srcurl=saveFile(m_shenfen,"shenfenzheng",m_shenfenFileName);
if(srcurl!=null){
this.message="\"srcurl\":\""+srcurl+"\"";
}
}
if(this.message!=null){
this.message="{"+this.message+"}";

}
textmessage=new ByteArrayInputStream(message.getBytes("GBK"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

return Action.SUCCESS;
}
verifyJpg函数验证信息有效性,参数文件名,文本域name属性;改变verification参数作为标记;
public void verifyJpg(String getname,File file){
// 验证上传文件

if (file != null&&getname!=null) {

if (!ProjectUtils.isJpgTypeAllowed(this.getExt(getname))) {//取文件后缀名
//文件类型静态函数,参数为文件后缀名
if(this.message!=null){
this.message=this.message+",\"error\":\"上传文件类型错误!\"";
}else{
this.message="\"error\":\"上传文件类型错误!\"";
}
this.setVerification(false);
} else {
if (file.length() > (1024 * 300)) {
//文件超过300K
if(this.message!=null){
this.message=this.message+",\"error\":\"文件超出指定大小!\"";
}else{
this.message="\"error\":\"文件超出指定大小!\"";
}
this.setVerification(false);
}
}
}else{
//没有上传文件
if(this.message!=null){
this.message=this.message+",\"error\":\"没有上传文件!\"";
}else{
this.message="\"error\":\"没有上传文件!\"";
}
this.setVerification(false);
}
}
文件类型验证用函数
public String getExt(String filename) {
if (ext == null&&filename!=null) {
ext = filename.substring(filename.lastIndexOf(".") + 1);
}
return ext;//返回文件后缀名
}
public static boolean isJpgTypeAllowed(String ext) {
String[] allowed = {"jpg","jpeg","bmp"};
int i = 0;
for (i = 0; i < allowed.length; i++) {
if (allowed[i].equalsIgnoreCase(ext)) {
break;
}
}
if (i == allowed.length) {
return false;
}
return true;//验证文件类型是否符合条件,返回true或者false
}

验证通过后保存,参数为Verification为true进行存储:
存储函数saveFile返回srcurl(为文件名),参数3个,文本域name属性,自定义新名字(用于上面action执行验证hashMap中是否存在该名字),文本域名字(shenfenFileName)
public String saveFile(File file,String newname,String getname){
if (file!=null) {
???? String filename=newname+GenerateDistinctFileName(getname);//GenerateDistinctFileName函数生成随机文件名;
???? String path = ServletActionContext.getServletContext().getRealPath(Constant.FILE_PATCH)+"\\"+Upd;
if(saveFile(file, path, filename,newname,getname))
//this.realpath=path+"\\"+filename;
return filename;
}
return null;//否则返回null;
}

内saveFile方法,有5个参数;文本域name属性,存储路径,文件名(自定义新名字(确定)+随机生成名(函数,参数为文本域内容shenfenFileName)),自定义新名字,文本域名字(及文本域内容)
返回值为参数文件名。

// 保存word文件,成功后返回保存路径,失败返回null
public boolean saveFile(File file, String path, String filename,String newname,String getname) {
try {
if (file != null) {
InputStream is = new FileInputStream(file);
File checkpath = new File(path);
//判断路径是否存在。如果不存在则创建
if (!checkpath.exists())
{
checkpath.mkdir();
}
File outfile = new File(path, filename);

OutputStream os = new FileOutputStream(outfile);
byte[] buffer = new byte[400];
int length = 0;
while ((length = is.read(buffer)) != -1) {
os.write(buffer, 0, length);
}
is.close();
os.close();

//保存对象到SESSION
saveSession(filename,path,newname,getname);
}

} catch (Exception e) {
e.printStackTrace();
return false;
}
System.out.println("file name is :" + filename);
return true;
}

?? read参数,write参数 ,都是用于图像,声音输入输出所必须的,,

存入session
saveSession 方法,参数 文件组合名,路径,自定义名字,文本域名
//放入SESSION步骤
private void saveSession(String filename,String path,String type,String getname)
{
UserFileSubmitReq userfile = new UserFileSubmitReq();
HashMap hash= (HashMap)getSession().getAttribute(Constant.SESSION_JG_FILE);
if(hash == null)
{
hash = new HashMap();
}
userfile.setFileCode(filename.replace(".jpg","").replace(".jpeg", "").replace(".bmp", ""));
userfile.setFileName(getname.replaceAll(".jpg", "").replaceAll(".jpeg", "").replace(".bmp", ""));
userfile.setFileType("."+getExt(getname));
//机构投资者
userfile.setFileUse("04");
userfile.setInpath(path+"\\"+filename);
hash.put(type,userfile);


getSession().setAttribute(Constant.SESSION_JG_FILE,hash);
}

原文地址:https://www.cnblogs.com/zhangchenglzhao/p/3022546.html