1.文件上传必备三属性
private File imgFile;//上传文件
private String imgFileFileName;//文件的名称
private String imgFileContentType;//文件的类型
2.提供的方法
public void setImgFile(File imgFile) {
this.imgFile = imgFile;
}
public void setImgFileFileName(String imgFileFileName) {
this.imgFileFileName = imgFileFileName;
}
public void setImgFileContentType(String imgFileContentType) {
this.imgFileContentType = imgFileContentType;
}
3.后台核心代码
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class ImageAction extends BaseAction<Promotion> {
private File imgFile;
private String imgFileFileName;
private String imgFileContentType;
public void setImgFile(File imgFile) {
this.imgFile = imgFile;
}
public void setImgFileFileName(String imgFileFileName) {
this.imgFileFileName = imgFileFileName;
}
public void setImgFileContentType(String imgFileContentType) {
this.imgFileContentType = imgFileContentType;
}
@Action(value = "image_upload", results = { @Result(name = "success", type = "json") })
public String upload() throws IOException {
System.out.println("文件:" + imgFile);
System.out.println("文件名:" + imgFileFileName);
System.out.println("文件类型:" + imgFileContentType);
文件:D:\JavaEE\workspace_maven\bos2.0\bos_management\target\tomcat\work\localEngine\localhost\bos_management \upload_05a01840_4c64_4134_b227_1cf57989c869_00000001.tmp
文件名:wy.jpg
文件类型:image/jpeg
// 获取上传路径
String savePath = ServletActionContext.getServletContext().getRealPath("/uoload/");
System.out.println(savePath);
String saveUrl = ServletActionContext.getRequest().getContextPath() + "/uoload/";
System.out.println(saveUrl);
D:\JavaEE\workspace_maven\bos2.0\bos_management\src\main\webapp\uoload
/bos_management/uoload/
// 生成随机图片名
UUID uuid = UUID.randomUUID();
String randomFileName = imgFileFileName = uuid + imgFileFileName.substring(imgFileFileName.lastIndexOf("."));
File destFile = new File(savePath + "/" + randomFileName);
// 保存图片
FileUtils.copyFile(imgFile, destFile);
// 通知浏览器文件上传成功
Map<String, Object> result = new HashMap<>();
result.put("error", 0);
result.put("url", saveUrl + randomFileName);// 返回相对路径
ActionContext.getContext().getValueStack().push(result);
return SUCCESS;
}
}
4.前台核心代码
KindEditor.ready(function(K) {
window.editor = K.create('#descriptionId',{
allowFileManager:true,
uploadJson:'../../image_upload.action',
fileManagerJson:'../../image_manage.action'
});
});
5.图片空间获取核心代码
@Action(value = "image_manage", results = { @Result(name = "success", type = "json") })
public String manage() {
// 根目录路径,可以指定绝对路径
String rootPath = ServletActionContext.getServletContext().getRealPath("/upload/");
// 根目录路径,可以 指定绝对路径
String rootUrl = ServletActionContext.getRequest().getContextPath() + "/upload/";
// 图片扩展名
String[] fileTypes = new String[] { "gif", "jpg", "jpeg", "png", "bmp" };
// 遍历目录取的文件信息
List<Map<String, Object>> fileList = new ArrayList<>();
// 当前上传目录
File currentPathFile = new File(rootPath);
if (currentPathFile.listFiles() != null) {
for (File file : currentPathFile.listFiles()) {
Map<String, Object> hash = new HashMap<String, Object>();
String fileName = file.getName();
if (file.isDirectory()) {
hash.put("is_dir", true);
hash.put("has_file", (file.listFiles() != null));
hash.put("filesize", 0L);
hash.put("is_photo", false);
hash.put("filetype", "");
} else if (file.isFile()) {
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
hash.put("is_dir", false);
hash.put("has_file", false);
hash.put("filesize", file.length());
hash.put("is_photo", Arrays.<String> asList(fileTypes).contains(fileExt));
hash.put("filetype", fileExt);
}
hash.put("filename", fileName);
hash.put("datetime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(file.lastModified()));
fileList.add(hash);
}
}
Map<String, Object> result = new HashMap<>();
result.put("moveup_dir_path", "");
result.put("current_dir_path", rootPath);
result.put("current_url", rootUrl);
result.put("total_count", fileList.size());
result.put("file_list", fileList);
ActionContext.getContext().getValueStack().push(result);
return SUCCESS;
}