1、在ajaxfilesupload.js文件中,修改如下代码:
2、上传代码展示:
<script type="text/javascript"src="<%=path %>/js/upload/ajaxfilesupload.js"></script>
Html代码:
<inputtype='file' id='file' name='file' runat='server' accept='image/png,image/gif,image/jpg,image/jpeg' capture='camera'οnchange='handleFiles(this)'/>
Js代码:
多文件上传需获取所有file的id:
vararrId = [];
$("input[name='fileupload']").each(function(){
arrId.push($(this).attr("id"));
});
$.ajaxFileUpload({
url :path+'/person/editheadpic.do',
secureuri : false,//安全协议
fileElementId:,//id 'file'/arrId
dataType:'text',
type : 'POST',
success : function(data, status) {
varjson = "";
var ua = navigator.userAgent.toLowerCase();
if(ua.indexOf('ucbrowser') > -1&& ua.match('android')){
json =data.substring(data.lastIndexOf(">")+4);
json = eval('(' + json +')');
}else {
json =jQuery.parseJSON(jQuery(data).text());
}
//返回结果的相应操作
}
});
Java代码:
//单文件上传上传文件
MultipartHttpServletRequest multipartRequest= (MultipartHttpServletRequest) req;
MultipartFile file =multipartRequest.getFile("file");
if (file.getSize() > 0)
{
// 文件名称
String fileName =file.getOriginalFilename();
// 本地存储路径
SimpleDateFormat sdf =new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sdf2 = newSimpleDateFormat("HHmmssSSS");
String dateStr =sdf2.format(new Date());
String path =req.getSession().getServletContext().getRealPath("") + File.separator+ "wap"
+ File.separator+ "user" + File.separator + sdf.format(new Date()) + File.separator;
File pathfile = newFile(path);
// 如果文件目录不存在,则新建
if (!pathfile.exists()&& !pathfile.isDirectory())
{
pathfile.mkdirs();
}
// 先存入本地
String fpath = path +dateStr + fileName.substring(fileName.lastIndexOf("."));
File newFile = newFile(fpath);
if (!newFile.exists())
{
newFile.createNewFile();
}
// 上传文件
FileOutputStreamoutputStream = new FileOutputStream(newFile);
FileCopyUtils.copy(file.getInputStream(), outputStream);
// 指定对应模块的文件夹
Map<String, String>param = new HashMap<String, String>();
param.put("fileTypeName", "center");
// 上传图片后的图片地址
String picUrl =UploadUtil.uploadFile(fpath, "img", req, param);
//多文件上传
// 转型为MultipartHttpRequest(重点的所在)
MultipartHttpServletRequestmultipartRequest = (MultipartHttpServletRequest) req;
try
{
// 循环上传图片
for (Iterator<String> it =multipartRequest.getFileNames(); it.hasNext();)
{
String key = it.next();
MultipartFile file =multipartRequest.getFile(key);
if(file.getOriginalFilename().length() > 0)
{
// 文件名称
String fileName =file.getOriginalFilename();
// 先存入本地
String fpath = path +sdf2.format(nowDate) + key +fileName.substring(fileName.lastIndexOf("."));
File newFile = new File(fpath);
if (!newFile.exists())
{
newFile.createNewFile();
}
FileOutputStreamoutputStream = new FileOutputStream(newFile);
FileCopyUtils.copy(file.getInputStream(), outputStream);
// 指定对应模块的文件夹
Map<String, String>param = new HashMap<String, String>();
param.put("fileTypeName", "return");
// 上传图片后的图片地址
String picUrl =UploadUtil.uploadFile(fpath, "img", req, param);
if(!StringTools.isNullOrEmpty(picUrl))
{
affixurl +="," + picUrl;
}
}
}
}
catch (IOException e)
{
logger.error("上传文件流异常",e);
}
// 判断非空,截取第一逗号
if(!StringTools.isNullOrEmpty(affixurl))
{
affixurl = affixurl.substring(1);
}