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;
}