使用kindeditor批量上传图片失败

忘川y 2015-09-06 02:44:08
上传图片时 将图片静态存储在nginx服务器中,但是nginx里存在资源,却显示上传失败,但是也有响应信息,请求路径也返回200
下面是后台代码
/**
* 图片上传
*/
@Controller

public class PicUploadController {

private static final Logger LOGGER = LoggerFactory.getLogger(PicUploadController.class);

@Autowired
private PropertieService propertieService;

private static final ObjectMapper mapper = new ObjectMapper();

// 允许上传的格式
private static final String[] IMAGE_TYPE = new String[] { ".bmp", ".jpg", ".jpeg", ".gif", ".png" };

@RequestMapping(value = "upload", method = RequestMethod.POST)
@ResponseBody
public String upload(@RequestParam("uploadFile") MultipartFile uploadFile, HttpServletResponse response)
throws Exception {

// 校验图片格式
boolean isLegal = false;
for (String type : IMAGE_TYPE) {
if (StringUtils.endsWithIgnoreCase(uploadFile.getOriginalFilename(), type)) {
isLegal = true;
break;
}
}

// 封装Result对象,并且将文件的byte数组放置到result对象中
PicUploadResult fileUploadResult = new PicUploadResult();

// 状态,0-合法,1-不合法
fileUploadResult.setError(isLegal ? 0 : 1);

// 文件新路径
String filePath = getFilePath(uploadFile.getOriginalFilename());

if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Pic file upload .[{}] to [{}] .", uploadFile.getOriginalFilename(), filePath);
}

// 生成图片的绝对引用地址
String picUrl = StringUtils.replace(
StringUtils.substringAfter(filePath, propertieService.REPOSITORY_PATH), "\\", "/");
fileUploadResult.setUrl(propertieService.IMAGE_BASE_URL + picUrl);


File newFile = new File(filePath);

// 写文件到磁盘
uploadFile.transferTo(newFile);

// 校验图片是否合法
isLegal = false;
try {
BufferedImage image = ImageIO.read(newFile);
if (image != null) {
fileUploadResult.setWidth(image.getWidth() + "");
fileUploadResult.setHeight(image.getHeight() + "");
isLegal = true;
}
} catch (IOException e) {
}

// 状态
fileUploadResult.setError(isLegal ? 0 : 1);

if (!isLegal) {
// 不合法,将磁盘上的文件删除
newFile.delete();
}

//设置响应内容的类型,文本类型
response.setContentType("text/html");
String json=mapper.writeValueAsString(fileUploadResult);//将java对象转换为json字符串
System.out.println(json);
return json;
}

private String getFilePath(String sourceFileName) {
String baseFolder = propertieService.REPOSITORY_PATH + File.separator + "images";
Date nowDate = new Date();
// yyyy/MM/dd
String fileFolder = baseFolder + File.separator + new DateTime(nowDate).toString("yyyy")
+ File.separator + new DateTime(nowDate).toString("MM") + File.separator
+ new DateTime(nowDate).toString("dd");
File file = new File(fileFolder);
if (!file.isDirectory()) {
// 如果目录不存在,则创建目录
file.mkdirs();
}
// 生成新的文件名
String fileName = new DateTime(nowDate).toString("yyyyMMddhhmmssSSSS")
+ RandomUtils.nextInt(100, 9999) + "." + StringUtils.substringAfterLast(sourceFileName, ".");
return fileFolder + File.separator + fileName;
}

}
...全文
305 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
忘川y 2015-09-06
  • 打赏
  • 举报
回复
大牛们别捉迷藏啊!!!
忘川y 2015-09-06
  • 打赏
  • 举报
回复
人勒|?
下载代码方式:https://pan.quark.cn/s/a4b39357ea24 全国计算机等级考试二级教程《Python语言程序设计》(2018年版)被视为针对Python入门者和备考人员的核心学习材料。该资料汇总了教材内的所有编程练习答案,这些答案在Python 3.5.3环境中经过实际执行测试,从而保障了代码的准确性和应用价值。备考人员借助这些答案,能够评估自身的学习进度,并深入理解和熟练掌握Python编程的基础原理与方法。 1. Python基础理论:Python作为一门高级编程语言,因其简明扼要的语法结构和卓越的功能表现而备受推崇。基础内容涵盖了变量、数据种类(例如整型、浮点型、字符串、布尔型、列表、元组、字典、集合)、逻辑控制(比如条件判断if-else,循环控制for、while)、函数的声明及使用、模块的引入等。 2. Python高级应用:Python的进阶内容涉及异常管理(try-except-finally结构),类与实例(面向对象编程的基础,包含类的构造、对象的生成、属性与方法的运用、继承机制、多态表现),装饰器(用于调整函数或类的功能特性),上下文管理器(借助with语句实现资源管理)等。 3. 数据文件处理:Python配备了全面的文件操作功能,涉及文件的开启、数据读写、关闭操作,以及多种操作模式(例如r模式用于读取,w模式用于写入,a模式用于追加内容等)。此外,还包括文本文件与二进制文件的转换处理,以及文件的位置调整、复制和删除等操作。 4. 标准库的运用:Python的标准库资源极为丰富,比如os模块提供操作系统接口,sys模块用于获取系统参数,random模块可用于生成随机数值,datetime模块负责处...

81,110

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧