首页 > Web开发 > 详细

easyUI + swfupload 多附件上传功能

时间:2016-12-28 14:43:28      阅读:167      评论:0      收藏:0      [点我收藏+]

public void UPLOADFILED() {
Date dt = new Date(System.currentTimeMillis());
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String fileName = sdf.format(dt);
int index = getUploadFileName().lastIndexOf(".");
//得到文件扩展名
String extendName = getUploadFileName().substring(index);
String path = getRootPath()+"up\\";
//String sessionId = getRequest().getSession().getId();
String filename = fileName + extendName;
Json j = uploadFile(filename, path, 200 * 1024 *1024, true);
try {
String json = JSON.toJSONStringWithDateFormat(j,"yyyy-MM-dd");
ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
ServletActionContext.getResponse().getWriter().write(json);
ServletActionContext.getResponse().getWriter().flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//writeJson(json);
}


/*public String uploadFile(String path){
return uploadFile(getUploadFileName(), path, 500 * 1024, false);
}*/

/**
* 上传文件
* @param filename 文件名
* @param path 文件保存路径
* @param maxSize 上传文件的最大大小
* @param overwrite 是否覆盖已存在的文件
* @return
*/
public Json uploadFile(String filename, String path, long maxSize, boolean overwrite){
Json j = new Json();
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
String fileName = filename;
String msg = "文件上传成功!";
if (! overwrite) {
//检查并得到新的保存文件名,防止重名后覆盖已存在的文件
fileName = FileUtils.checkFileName(filename, path);
if (FileUtils.isFileExist(fileName, path)) {
/*FileDownloadUtils.downloadJSON("{success:false, msg:‘上传文件名已存在,请改名后重新上传!‘}",
getResponse());*/
msg = "上传文件名已存在,请改名后重新上传!";
return null;
}
}
if (upload == null) {
/*FileDownloadUtils.downloadJSON("{success:false, msg:‘文件名及路径名有问题,请修改后重新上传!‘}",
getResponse());*/
msg = "文件名及路径名有问题,请修改后重新上传!";
return null;
}
try {
if (upload.length()> maxSize) {
/*FileDownloadUtils.downloadJSON("{success:false, msg:‘上传文件不能大于" + maxSize +",请修改后重新上传!‘}",
getResponse());*/
msg = "上传文件不能大于" + maxSize +",请修改后重新上传!";
return null;
}
FileUtils.uploadForName(fileName, path, upload);
} catch (IOException e) {
e.printStackTrace();
/*FileDownloadUtils.downloadJSON("{success:false, msg:‘文件上传失败!‘}",
getResponse());*/
msg = "文件上传失败!";
return null;
}
j.setSuccess(true);
j.setMsg(msg);
jsonObject.put("name", fileName);
jsonObject.put("type", FileUtils.converContentType(getUploadContentType()));
jsonObject.put("size", upload.length());
jsonArray.add(jsonObject);
j.setObj(jsonArray);
/*FileDownloadUtils.downloadJSON(
"{success:true, msg:‘文件上传成功!‘, " +
"file: {name:‘" + fileName +
"‘, type:‘" + FileUtils.converContentType(getUploadContentType()) +
"‘, size:" + upload.length() + "}}",
getResponse());*/

//errMessage="文件上传成功!";
//success = true;
return j;
}

easyUI + swfupload 多附件上传功能

原文:http://www.cnblogs.com/huangf714/p/6229320.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!