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

来源:互联网 发布:力高答题软件 编辑:程序博客网 时间:2024/05/29 16:31

在使用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就正常了。

阅读全文
0 0
原创粉丝点击