`
yanwt
  • 浏览: 97844 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

xheditor-文件上传-Spring3 MVC-支持html5-application/octet-stream

阅读更多
xheditor1.1.5 版本文件上传使用html5-application/octet-stream
Spring3 MVC之前写的上传程序无法支持
@RequestMapping(value = "/upload")
    public String uploadFile(Model m, MultipartHttpServletRequest request) throws IOException {
        String path = UploadController.class.getResource("/").getPath().split("WEB-INF")[0] + "upload/";
        Iterator<String> iterator = request.getFileNames();
        while (iterator.hasNext()) {
            String next = iterator.next();
            List<MultipartFile> files = request.getFiles(next);
            for (int i = 0; i < files.size(); i++) {
                if (!files.get(i).isEmpty()) {
                    byte[] bytes = files.get(i).getBytes();
                    String uploadFile = UploadUtils.generateFilename(path, UploadUtils.getExtension(files.get(i).getOriginalFilename(), "jpg"));
                    File file = new File(uploadFile);
                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write(bytes);
                    fos.close();
                    m.addAttribute("url", uploadFile.replace(path, "/upload"));
                    m.addAttribute("local", uploadFile);
                }
            }
        }
        return "upload";
    }


需要改造如下:
@RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String upload(Model m, HttpServletRequest request) throws IOException {
        String path = UploadController.class.getResource("/").getPath().split("WEB-INF")[0] + "upload/";
        if ("application/octet-stream".equals(request.getContentType())) { //HTML 5 上传
            try {
                String dispoString = request.getHeader("Content-Disposition");
                int iFindStart = dispoString.indexOf("filename =\"") + 10;
                int iFindEnd = dispoString.indexOf("\"", iFindStart);
                String sFileName = dispoString.substring(iFindStart, iFindEnd);
                int i = request.getContentLength();
                byte buffer[] = new byte[i];
                int j = 0;
                while (j < i) { //获取表单的上传文件
                    int k = request.getInputStream().read(buffer, j, i - j);
                    j += k;
                }
                if (buffer.length >= 0) { //文件是否为空
                    String uploadFile = UploadUtils.generateFilename(path, UploadUtils.getExtension(sFileName, "jpg"));
                    File file = new File(uploadFile);
                    OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true));
                    out.write(buffer);
                    out.close();
                    m.addAttribute("url", uploadFile.replace(path, "/upload"));
                    m.addAttribute("local", uploadFile);
                }
            } catch (Exception ex) {
                m.addAttribute("err", ex.getMessage());
            }
        }
        return "upload";
    }
分享到:
评论
3 楼 hesai_vip 2013-05-30  
请问这个返回upload页面之后怎么做呢?我的总是报错啊
返回的错误内容为:



url : /upload/4.jpg

local: /D:/ourchem/hs-project/.metadata/.plugins/org.eclipse.wst.server.core/tmp3/wtpwebapps/ourchem-website/upload/4.jpg



不知道如何处理,请教!!!
2 楼 鐜嬫旦 2012-10-17  
UploadUtils这个方法也贴下啊,,,
1 楼 wendal 2012-06-02  
Good!

相关推荐

    xheditor-1.2.2.zip

    xheditor-1.2.2.zip

    xheditor-0.9.8-zh-cn

    xheditor-0.9.8-zh-cn.zip 编辑器xheditor-0.9.8-zh-cn.zip 编辑器xheditor-0.9.8-zh-cn.zip 编辑器xheditor-0.9.8-zh-cn.zip 编辑器xheditor-0.9.8-zh-cn.zip 编辑器

    最土团购商业wap版测试可以

    inflating: zuitu/static/js/xheditor/xheditor_emot/msn/3.gif inflating: zuitu/static/js/xheditor/xheditor_emot/msn/30.gif inflating: zuitu/static/js/xheditor/xheditor_emot/msn/31.gif inflating: ...

    xheditor-1.1.13在asp.net中使用

    xheditor在asp.net中使用时,如果textbox控件在UpdatePanel使用...在本文件在包含完整的xheditor-1.1.13及其demos。在demos文件夹里的Default2.aspx及Default2.aspx.cs是解决textbox控件在UpdatePanel的正常使用方法。

    xheditor-1.1.1编辑器下载

    xheditor编辑器.................xheditor编辑器.................xheditor编辑器.................xheditor编辑器.................xheditor编辑器.................

    xheditor-1.1.7 编辑器插件

    xheditor-1.1.7 编辑器插件 包含xheditor所含关键文件及jquery,导入web工程即可使用

    最新版本xheditor-v1.2.2

    最新版本xheditor-v1.2.2

    xheditor-1.1.9文件

    文本编辑器文件包 具体介绍见博客 文本编辑器文件包

    xheditor-1.1.6

    xheditor-1.1.6,html编辑器

    xheditor-1.2.1(demo,js,api,doc)

    一款很好用的编辑器,xheditor-1.2.1(demo,js,api,doc),官网上下载的最新代码,方便查阅参考

    xheditor-1.1.14

    html5Upload:是否开启HTML5上传支持 参数值:true(允许),false(不允许),默认为true允许 说明:本功能需要浏览器支持HTML5上传 备注:1.0.0 Final新添加 upMultiple:允许一次上传多少个文件 参数值:大于0的数值,...

    spring3MVC 框架demo

    spring3MVC框架基本配置案例,里面两个功能案例,上传下载功能和使用xheditor发布消息功能

    xhEditor编辑器

    将下载的压缩文件解压缩,上传其中的xheditor-zh-cn.min.js以及xheditor_emot、xheditor_plugins和xheditor_skin三个文件夹到网站相应文件夹中。 注:如果您网站中没有使用jQuery框架,也请一并上传jquery文件夹中的...

    xheditor-1.0.0-final

    这是一个富文本编辑器 使用起来很简单 几句代码就搞定了 当然也相对简单。

    ASP.NET应用Xheditor上传图片 jquery-1.11.0

    xheditor-1.2.1 上传图片 jquery版本:jquery-1.11.0 ASP.NET环境

    xheditor-1

    xheditor-1.1.5 测试可用 感觉很行 就是比较简单,对网页要求不大,主要编辑网站的可以考虑哦

    xheditor-1.2.1.7z

    xhEditor是一个基于jQuery开发的简单迷你并且高效的可视化HTML编辑器,基于网络访问并且兼容IE 6.0+,Firefox 3.0+,Opera 9.6+,Chrome 1.0+,Safari 3.22+。

    xhEditor编缉器下使用java上传的类文件

    xhEditor编缉器,用于java开发时,上传文件,同时解决了在火狐上使用时报错的问题,主要是因为火狐上传时使用的是HTML5,如何解决请花1分吧!! 记得把xheditor里的上传改成servlet的路径呀!!

    xheditor 1.2.2.zip

    内置强大的Ajax上传,包括HTML4和HTML5上传支持(多文件上传、真实上传进度及文件拖放上传),追求完美的用户上传体验。 Word完美支持 实现Word代码自动检测并清理,提供高效完美的Word代码过滤方案,生成代码最优化...

    xheditor-1.1.11

    xhEditor是一个基于jQuery开发的简单迷你并且高效的在线可视化HTML编辑器,基于网络访问并且兼容IE 6.0+,Firefox 3.0+,Opera 9.6+,Chrome 1.0+,Safari 3.22+。 xhEditor完全基于Javascript开发,您可以应用在任何的...

Global site tag (gtag.js) - Google Analytics