日期:2014-05-16  浏览次数:20810 次

java 用apache fileupload上传文件
MLGB,最简单易用的文件上传方式有木有,这个都看不懂可以去xi了




页面:

<form action="mlgb.shtml" enctype="multipart/form-data"  method="post">
    <input type="file" name="file"/>
    <input type="text" name="filename"/>
    <input type="submit"/>
</form>





java处理逻辑

@RequestMapping(value = "documentationupload.shtml")
    public String documentationupload(HttpServletRequest request, HttpServletResponse res) throws Exception {
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload uploader = new ServletFileUpload(factory);
        List<FileItem> list = uploader.parseRequest(request);
        for (FileItem item : list) {
            if (item.isFormField()) {
                // 处理普通表单字段
                String field = item.getFieldName();//这个是name
                String value = item.getString("UTF-8");//这个是name对应的值
                //对数据进行逻辑处理
            } else {
                //将文件保存到指定目录
                String fileName = item.getName();//文件名称
                String filepath = pathUtil.getWebRoot() + uploadpath + fileName;
                item.write(new File(filepath));//执行保存
            }
        }
        return "/admin/documentation.jsp";
    }