package cn.core.mvc;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.upload.TempFile;
import org.nutz.mvc.upload.UploadAdaptor;
import cn.admin.bean.SysUploadfiles;
import cn.core.util.BaseModule;
@IocBean
public class Uploader extends BaseModule {
@At("/upload")
@AdaptBy(type = UploadAdaptor.class, args = { "ioc:upload" })
@Ok("raw:json")
@Filters
public String Upload(@Param("file") TempFile tf, @Param("formData") HashMap<String, Object> data,
ServletContext sc) {
long answerId = System.currentTimeMillis();
Map<String, Object> map = new HashMap<>();
if (tf == null || "".equals(tf)) {
map.put("code", "-1");
map.put("result", "上传失败!");
return Json.toJson(map);
}
try {
String[] postfix = tf.getSubmittedFileName().split("\\.");
String path = sc.getRealPath("/").replace("\\pac\\", "");
// 上传商品图片
if ("hospitallogo".equalsIgnoreCase(data.get("filetype").toString())) {
path += "/upload/logo/";
// 删除物理文件
if (!(data.get("oldhospitallogo") == null
|| "".equalsIgnoreCase(data.get("oldhospitallogo").toString()))) {
String filename = sc.getRealPath("/").replace("\\pac\\", "")
+ data.get("oldhospitallogo").toString();
File file = new File(filename);
if (file.exists()) {
file.delete();
}
}
File temp = new File(path);
if (!temp.exists()) {
temp.mkdirs();
}
tf.write(temp.getPath() + "/" + answerId + "." + postfix[postfix.length - 1]);
tf.delete();
map.put("code", "1");
map.put("result", "/upload/logo/" + answerId + "." + postfix[postfix.length - 1]);
return Json.toJson(map);
}
map.put("code", "-1");
map.put("msg", "文件上传失败!");
return Json.toJson(map);
} catch (Exception e) {
e.printStackTrace();
map.put("code", "-1");
map.put("msg", "文件上传失败!");
return Json.toJson(map);
}
}
//
@At("/delfile")
public Object delfile(@Param("obj") HashMap<String, Object> data, ServletContext sc) {
try {
String filename = sc.getRealPath("/").replace("\\pac\\", "") + data.get("fileurl");
File file = new File(filename);
if (file.exists()) {
file.delete();
}
SysUploadfiles ufile = new SysUploadfiles();
ufile.setFileid(Long.valueOf(data.get("fileid").toString()));
super.dao.delete(ufile);
return super.nutzRet(1, "文件删除成功!", "");
} catch (Exception e) {
e.printStackTrace();
return super.nutzRet(-1, "文件删除失败!", "");
}
}
}
微信端的上传程序
function previewImages(file) {
var MAXWIDTH = 1000;
var MAXHEIGHT = 1200;
for (var i = 0; i < file.files.length; i++) {
if (file.files && file.files[i]) {
$.post(https+"/upload",{"file": file.files[i],"formData":{"type":"pac"}},function(rs){
//$(file).parent().prev().append('<li onclick="removeimg(this)" class="weui-uploader__file" style="background-image:url('+evt.target.result+')"><input value="'+rs.src+'" type="hidden" name="files" /></li>');
},'json');
/*
var reader = new FileReader();
reader.onload = function (evt) {
$.post(https+"/upload",{imgbase64: evt.target.result},function(rs){
$(file).parent().prev().append('<li onclick="removeimg(this)" class="weui-uploader__file" style="background-image:url('+evt.target.result+')"><input value="'+rs.src+'" type="hidden" name="files" /></li>');
},'json');
};
reader.readAsDataURL(file.files[i]);
console.log(file.files[i]);
*/
}
}
}