zoukankan      html  css  js  c++  java
  • from 表单上传文件和下载?

    from表单上传单个文件的方法。  分为三个部分,简单演示。

    一部分 表单上传文件

    <%--
      Created by IntelliJ IDEA.
      User: Administrator
      Date: 2019/8/9
      Time: 9:41
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <%--
              1.from标签中  指定路径   post提交  
              2.enctype属性:上传文件类型的数据    
              3.multipart/form-data  多媒体表单数据的格式 
              4.type file 浏览本地文件     
     --%>
    <body>
        <form method="post" action="../shang" enctype="multipart/form-data">
            <div> 文件<input type="file" name="file01"> </div>
            <div> <input type="submit" value="提交">  </div>
        </form>
    </body>
    </html>

    二部分  后台控制器

    package com.aaa.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.multipart.MultipartFile;
    
    import javax.servlet.http.HttpServletRequest;
    import java.io.File;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    @Controller
    public class DemoShangChuang {
            @RequestMapping("/demo")
            public String upload(@RequestParam("file12")  MultipartFile  file,Model model, HttpServletRequest request) throws IOException {
                            //先判断  上传的文件是否为空!
                            if (!file.isEmpty()) {
    
                                    //1.明确目标位置 是一个绝对路径  将文件传送到的地方?
                                    String path = request.getSession().getServletContext().getRealPath("/static/upload");
    
                                    //2.  2.1获取原始的文件名   2.2也可以自定义文件名(根据时间戳)
                                    String fileName = file.getOriginalFilename();
    
                                    //9.新的文件名?   文件名+文件后缀
                                     //新的文件名的 名字 通过日期函数 生成
                                    //只需要  旧的文件名的后缀。   . 以后的文件
                                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                                    String prefix = simpleDateFormat.format(new Date());
    
                                    String suffix = fileName.substring(fileName.lastIndexOf("."));
                                    String newfilename = prefix + suffix;
                                    //  System.out.println("新的文件名"+newfilename);
    
                                    //3.目标文件的对象。
                                    File file1 = new File(path + "/" + newfilename);
    
                                    //4.获取父级文件,判断是否存在  没有就创建目录
                                    if (!file1.getParentFile().exists()) {
                                            file1.mkdirs();
                                    }
    
    
                                    //8.目标文件存在  就不要让他存在    就是说 不存在的文件  才让他上传。  注意 用了时间戳以后,同一文件名会不断修改,可以重复上传。
                                    if (!file1.exists()) {
                                            //5.传输文件的方法
                                            file.transferTo(file1);
    
                                            //6.将数据封装到model里面   然后再ok.jsp  通过 “上传文件:${file}”  知道上传的是那个文件
                                            model.addAttribute("file", newfilename);
                                    }
                            } else {   //  上传的是空文件 提示用户!
                                    model.addAttribute("file", "上传文件为空");
                            }
    
                    //7.跳转到指定的页面。
                    return "view/ok";
    
            }
    }

    三、jsp 接收部分

    <%--
      Created by IntelliJ IDEA.
      User: Administrator
      Date: 2019/8/5
      Time: 16:25
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>接收文件</title>
    </head>
    <body>
    <%-- 在jsp页面接收 我们上传的文件名。  --%>
        上传文件:${file}
    
    <%--将文件的路径放在这里  就可以将上传的文件下载。 --%>
    <a href="http://localhost:8848/zxf/static/upload/8.png">下载8.png</a>
    </body>
    </html>
  • 相关阅读:
    flex
    当前不会命中断点 源代码与原始版本不一致
    c setjmp longjmp
    VS 快捷键设置
    Lua C API 遍历 table
    lua class
    复习 C++ 中类的函数指针
    apache ab
    rabbitmq
    协程 coroutine
  • 原文地址:https://www.cnblogs.com/ZXF6/p/11340351.html
Copyright © 2011-2022 走看看