UploadController.java
2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.air.controller;
import java.io.File;
import java.io.FileFilter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.air.config.AbsController;
import com.jfinal.kit.Kv;
import com.teplot.common.Response;
import com.teplot.common.Utils;
/**
* Depiction:文件上传控制器
* <p>
* Modify:
* <p>
* Author: Kevin Lynn
* <p>
* Create Date:2016年6月3日 下午11:02:05
* <p>
*
* @version 1.0
* @since 1.0
*/
public class UploadController extends AbsController {
// 仅仅后台umeditor上传图片使用
public void uploadImg() {
getResponse().setContentType("text/html;charset=utf-8");
String destDir = Utils.getImgDir();
String fileName = Utils.productNo();
Response ret = upload("upfile", 2 * 1024 * 1024, "图片大小不能超过2MB,仅支持jpg和png格式", destDir, fileName);
renderHtml(ret.toJson());
}
// 仅仅后台kindeditor上传图片使用
public void uploadImage() {
getResponse().setContentType("text/html;charset=utf-8");
String destDir = Utils.getImgDir();
String fileName = Utils.productNo();
Response ret = upload("imgFile", 2 * 1024 * 1024, "图片大小不能超过2MB,仅支持jpg和png格式", destDir, fileName);
renderHtml(ret.toJson());
}
// 返回图片目录的所有图片
public void imageManager() {
String dirName = Utils.getImgDir();
String rootUrl = Utils.host() + "/upload/img/";
String[] fileTypes = new String[] { "gif", "jpg", "jpeg", "png", "bmp" };
File currentPathFile = new File(dirName);
List<Kv> fileList = new ArrayList<Kv>();
File[] files = currentPathFile.listFiles(new ImageFilter());
if (files != null) {
for (File file : files) {
Kv hash = new Kv();
String fileName = file.getName();
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
hash.set("is_dir", false);
hash.set("has_file", false);
hash.set("filesize", file.length());
hash.set("is_photo", Arrays.<String>asList(fileTypes).contains(fileExt));
hash.set("filetype", fileExt);
hash.set("filename", fileName);
hash.set("datetime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(file.lastModified()));
fileList.add(hash);
}
}
Kv result = new Kv();
result.set("moveup_dir_path", dirName);
result.set("current_dir_path", dirName);
result.set("current_url", rootUrl);
result.set("total_count", fileList.size());
result.set("file_list", fileList);
getResponse().setContentType("application/json; charset=UTF-8");
renderHtml(result.toJson());
}
class ImageFilter implements FileFilter {
String[] fileTypes = new String[] { "gif", "jpg", "jpeg", "png", "bmp" };
public boolean accept(File file) {
String fileExt = file.getName().substring(file.getName().lastIndexOf(".") + 1).toLowerCase();
return Arrays.<String>asList(fileTypes).contains(fileExt);
}
}
}