您的位置:首页 > 产品设计 > UI/UE

ueditor插件在ie上传图片出现下载JSON文件的解决

2017-06-15 18:22 726 查看
在使用ueditor富文本插件进行上传图片的调试时,自定义了上传图片的上传和返回路径,代码如下:

/**
* ueditor上传图片重写
*
* @param upfile
* @param request
* @return
* @throws IOException
*/
@CheckLogin(ResultTypeEnum.json)
@RequestMapping("/uploadImage")
@ResponseBody // 这里upfile是config.json中图片提交的表单名称
public Map<String, String> uploadImage(@RequestParam("upfile") CommonsMultipartFile upfile,
HttpServletResponse response) throws IOException {
// 文件原名称
String fileName = upfile.getOriginalFilename();
// 文件后缀名
String type = fileName.substring(upfile.getOriginalFilename().lastIndexOf("."));
// 为了避免重复简单处理
String nowName = DateUtil.format(new Date(), "yyyyMMdd") + "_" + new Date().getTime() + type;
if (!upfile.isEmpty()) {

// 上传位置路径
File targetPath = new File(fileProper.getString("uploadpath") + File.separator + "richTextPath"
+ File.separator + DateUtil.format(new Date(), "yyyy-MM-dd"));

if (!targetPath.exists()) {
targetPath.mkdirs();
}
String path0 = targetPath.getPath() + File.separator + nowName;
// 按照路径新建文件
File newFile = new File(path0);
// 复制
FileCopyUtils.copy(upfile.getBytes(), newFile);
}

// 返回结果信息(UEditor需要)
Map<String, String> map = new HashMap<String, String>();
// 是否上传成功
map.put("state", "SUCCESS");
// 现在文件名称
map.put("title", nowName);
// 文件原名称
map.put("original", fileName);
// 文件类型 .+后缀名
map.put("type", type);
// 文件路径
map.put("url", "/webCol/" + nowName + "/getImage");
// 文件大小(字节数)
map.put("size", upfile.getSize() + "");

response.setContentType("text/html");

return map;

}

/**
* ueditor读取文件重写
*/
@CheckLogin(ResultTypeEnum.json)
@RequestMapping("{imgName}/getImage")
public void readImg(@PathVariable("imgName") String imgName, HttpServletResponse response) throws Exception {
// 设置文件的返回类型
response.setContentType("image/*");

// 文件路径(windows下是\\,linux下是//,都必须是绝对路径)
String imgPath = fileProper.getString("uploadpath") + "richTextPath" + File.separator
+ DateUtil.format(new Date(), "yyyy-MM-dd") + File.separator + imgName;
// java中用File类来表示一个文件
File image = new File(imgPath);
// 测试这个文件路径是否存在(也就是这个文件是否存在)
if (!image.exists()) {
return;
}

// FileUtils.readFileToByteArray(File file)把一个文件转换成字节数组返回
response.getOutputStream().write(FileUtils.readFileToByteArray(image));
// java在使用流时,都会有一个缓冲区,按一种它认为比较高效的方法来发数据:
// 把要发的数据先放到缓冲区,缓冲区放满以后再一次性发过去,而不是分开一次一次地发.
// 而flush()表示强制将缓冲区中的数据发送出去,不必等到缓冲区满.
response.getOutputStream().flush();
response.getOutputStream().close();
}

最开始是没有在上传的重写方法中加下面这段代码
response.setContentType("text/html");
导致在IE下出现下载json的提示,加上之后在本地的IE下就可正常上传显示了。

之后将代码放到测试服务器,可能是因为本地和测试服务器环境的不一致,导致又出现了同样的问题。

在网上查找之后发现可以在spring-mvc.xml中加载注解驱动的地方设置支持的媒体类型,如下:

<!-- 加入注解驱动 -->
<mvc:annotation-driven validator="validator">
<mvc:message-converters>
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value><!-- 避免IE出现下载JSON文件的情况 -->
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

使用这个方法的最开始只是设置了property和一个value,就是text/html;charset=utf-8,但是发现项目中其他返回json数据的地方都出现了加载不出来的问题,之后使用list涵盖多个value就正常了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐