Jfinal上传Excel

我写的有什么问题吗?为什么一直是null??QQ20171215-120252.pngQQ20171215-120542.pngQQ20171215-120551.png

评论区

lyh061619

2017-12-15 20:07

你用了ajaxForm做上传,给个例子你参考下或许有得水一轮:
1、 controler层

public void add(){
String method = getRequest().getMethod();
if ("GET".equals(method)) {
Long id = getParaToLong(0);
Expense expense = expenseService.byId(id);
setAttr("expense", expense);
List categoryList = expenseCategoryService.list();
setAttr("categoryList", categoryList);
render("add.html");
} else if ("POST".equals(method)) {
List attachmentList = null;
try {
String basePath = PathKit.getWebRootPath() + "/upload";
String uploadPath = "/expense/attachment/" + DateTimeUtil.nowDate() + "/";
FileUtil.createDirectory(basePath + uploadPath);
attachmentList = getFiles(uploadPath, Cons.Size.MAX_POST_SIZE, "utf-8");

Expense expense = getBean(Expense.class, "", true);
expense.setUserId(getUserId());
expense.put("attachmentList", attachmentList);

Map res = new HashMap<>();
Long id = (Long) res.get("id");
boolean fn = expenseService.add(expense, res);
if (!fn) {
renderError();
return;
}
renderOk(id);
}catch(com.jfinal.upload.ExceededSizeException ex) {
renderJson("state", "上传图片只允许 200K 大小");
} catch(Exception e) {
if (attachmentList != null) {
for (UploadFile uploadFile : attachmentList) {
uploadFile.getFile().delete();
}
}
}
}
}

FileUtil.createDirectory()如果没有相关目录就创建个目录
/**
* 创建目录
* @param descDirName 目录名,包含路径
* @return 如果创建成功,则返回true,否则返回false
*/
public static boolean createDirectory(String descDirName) {
String descDirNames = descDirName;
if (!descDirNames.endsWith(File.separator)) {
descDirNames = descDirNames + File.separator;
}
File descDir = new File(descDirNames);
if (descDir.exists()) {
logger.debug("目录 " + descDirNames + " 已存在!");
return false;
}
// 创建目录
if (descDir.mkdirs()) {
logger.debug("目录 " + descDirNames + " 创建成功!");
return true;
} else {
logger.debug("目录 " + descDirNames + " 创建失败!");
return false;
}

}

2、 service
public boolean add(Expense expense, Map res) {
return Db.tx(()->{
expense.setClickCount(0);//浏览次数,初始为0
expense.setStatus(0);//发布状态:0未发布;1已经发布
expense.setActive(1);//用效标识:0无效;1有效;
if (sl.isEmpty(expense.getCreated())) {//如果创建时间为空,初始化为当前时间
expense.setCreated(new Date());
}
boolean fn = expense.save();
if (!fn) {
return fn;
}
res.put("id", expense.getId());


//保存附件映射记录
try {
fn = uploadFile(expense.get("attachmentList"), expense.getId());
} catch (IOException e) {
e.printStackTrace();
}
if (!fn) {
return fn;
}

return fn;
});
}

private boolean uploadFile(List attachmentList, Long expenseId) throws IOException {
boolean fn = false;
if (sl.isEmpty(attachmentList)) {
fn = true;
return fn;
}
//保存附件映射记录
for (UploadFile attr : attachmentList) {
ExpenseAttachment attachment = new ExpenseAttachment();
attachment.setDownloadCount(0);
attachment.setExpenseId(expenseId);
File srcFile = attr.getFile();
// attachment.setFileSize(attr.get);
String fileName = srcFile.getName();
String ext = fileName.substring(fileName.lastIndexOf("."));
if(".doc".equals(ext) || ".docx".equals(ext) || ".odt".equals(ext)) {//word
attachment.setFileType("word文档");
} else if (".xls".equals(ext) || ".xlsx".equals(ext) || ".ods".equals(ext) || "".equals(ext)) { //excel
attachment.setFileType("excel表格");
} else if (".ppt".equals(ext) || ".pptx".equals(ext) || ".odp".equals(ext)) {
attachment.setFileType("ppt演示文档");
} else if (".jpg".equals(ext) || ".jpeg".equals(ext) || ".png".equals(ext) || ".bmp".equals(ext)) { //price
attachment.setFileType("图片");
} else if (".txt".equals(ext)) {
attachment.setFileType("文本");
} else {
attachment.setFileType("未知");
}
if (!sl.isEmpty(fileName)){
fileName = fileName.substring(0, fileName.lastIndexOf("."));
attachment.setName(fileName);
}

//以注解是处理文件重命名
String path = attr.getUploadPath();
attachment.setPath(path);

fileName = null;
fileName = srcFile.getName(); //UUID.randomUUID().toString() + srcFile.getName().substring(srcFile.getName().lastIndexOf("."));
attachment.setFileName(fileName);
attachment.setCreated(new Date());

File saveFile = new File(PathKit.getWebRootPath() + path + fileName);
fn = saveFile.createNewFile();
if (!fn) {
return fn;
}
FileUtil.copyFile(srcFile, saveFile);
fn = attachment.save();
if (!fn) {
return fn;
}
}

return fn;
}

lyh061619

2017-12-15 20:08

另外,js
//提交(-兼容ie8上传必须使用jquery.form[3.51.0-2014.06.20] 的ajaxSubmit
$(".downs").click(function(){
$("#form1").ajaxSubmit({
type: "post",
url: "/admin/expense/add?xx=#(xx)&_url=#(_url)",
beforeSerialize:function() { //序列化提交数据之前的回调函数

},
beforeSubmit: function (d, f, o) { //提交成功后执行的回调函数
},
success: function (ret) {
if (ret.errorCode == 0) {
location.href="/admin/expense" + suffer;
} else if(ret.errorCode == 1){
Toast.error(ret.message);
} else {
location.href = "/admin/expense" + suffer;
}
},
clearForm:true,
restForm:true
});
return false;//想要阻止自动提交,必须return false;
});