• java Web 文件上传


    注意:请求实体过大的问题,请修改Nginx服务器的大小(百度参考413 Request Entity Too Large 的解决方法)
    jsp:<input type="file"  style="height: 114px; 100%;position: absolute;opacity: 0;" name="file1" id="file1" >

    $(function(){
    
            $("#file1").change(function(){
                //创建FormData对象
                var data = new FormData();
                //为FormData对象添加数据
                $.each($('#file1')[0].files, function(i, file) {
                         data.append('upload_file', file);
                 });
                $.ajax({
                     url:'${pageContext.request.contextPath}/uploadImage',
                     type:'POST',
                     data:data,
                     cache: false,
                     contentType: false,    //不可缺
                     processData: false,    //不可缺
                     success:function(data){
                        //显示追加
                         var imgUrl="/"+data;
                         var showImg = "<div class='img-item'>" +
                                 "<img src='" + imgUrl + "'>" +
                                 "<span class='close-item iconfont'></span>" +
                                 "</div>";
                         $("#addImg").before(showImg);
                         //数据追加
                         var inputImg = "<input type='hidden' name='postImage'value='" + imgUrl + "' >";
                         $("#typeId").after(inputImg);
                      }
                 });
             });
        });

    java后台部分:

     1     @RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
     2     @ResponseBody
     3     public String uploadImage(HttpServletRequest request) throws IOException {
     4         MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
     5         Iterator<String> iterator = multipartRequest.getFileNames();
     6         String fileName = "";
     7         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
     8         String dir = "upload/" + sdf.format(new Date()) + "/";
     9         String realPath = request.getSession().getServletContext().getRealPath("/");
    10         while(iterator.hasNext()){
    11             MultipartFile multipartFile = multipartRequest.getFile(iterator.next());
    12             if(multipartFile != null){
    13                 String fn = multipartFile.getOriginalFilename();
    14                 String suffix = fn.substring(fn.lastIndexOf("."));
    15                 fileName = dir  + Utils.getRandomStringByLength(6) + suffix;
    16                 String path = realPath + fileName;
    17                 path = path.replace("\", "/");
    18                 File f = new File(path);
    19                 if(!f.mkdirs()){
    20                     f.mkdir();
    21                 }
    22                 multipartFile.transferTo(f);
    23             }
    24         }
    25         return fileName;
    26     }
    27   //    上面方法需要用到下面的方法
    28   public String getRandomStringByLength(int length) {
    29         String base = "abcdefghijklmnopqrstuvwxyz0123456789";
    30         Random random = new Random();
    31         StringBuffer sb = new StringBuffer();
    32         for (int i = 0; i < length; i++) {
    33             int number = random.nextInt(base.length());
    34             sb.append(base.charAt(number));
    35         }
    36         return sb.toString();
    37     }
  • 相关阅读:
    [办公自动化]企业网IE多版本引发的网页无法访问
    [每天想一想]如果你的领导不懂技术,你该怎么办?
    [思考]我们应该怎样建设企业IT
    [读书笔记]流畅的Python(Fluent Python)
    [办公自动化]网件交换机管理软件
    PCRE函数简介和使用示例
    pcre7.0在vc6.0编译
    Luogu P2173 [ZJOI2012]网络
    tensorflow学习笔记2
    python数据可视化7
  • 原文地址:https://www.cnblogs.com/dwb91/p/6699110.html
Copyright © 2020-2023  润新知