SSM实现文件上传功能(全)


一、随便建立一个数据库表

 

 

 二、JSP页面部分

 1 <form id="upload-form" action="${pageContext.request.contextPath }/upload.action?id=${ADM.id}" method="post"
 2                                         enctype="multipart/form-data" class="am-form tpl-form-line-form">
 3                                         <div class="am-form-group">
 4                                             <label for="user-name" class="am-u-sm-3 am-form-label">标题 <span
 5                                                     class="tpl-form-line-small-title">Title</span></label>
 6                                             <div class="am-u-sm-9">
 7                                                 <input type="text" class="tpl-form-input" id="user-name"
 8                                                     placeholder="请输入标题文字" name="title_video">
 9                                                 <!-- <small>请填写标题文字10-20字左右。</small> -->
10                                             </div>
11                                         </div>
12 <!--
13                                         <div class="am-form-group">
14                                              <label for="user-email" class="am-u-sm-3 am-form-label">发布时间 <span
15                                                     class="tpl-form-line-small-title">Time</span></label> 
16                                             <div class="am-u-sm-9">
17                                                 <input type="text" class="am-form-field tpl-form-no-bg"
18                                                     placeholder="发布时间" data-am-datepicker="" readonly="" name="">
19 
20                                             </div>
21                                         </div>-->
22 
23                                         <div class="am-form-group">
24                                             <label for="user-phone" class="am-u-sm-3 am-form-label">视频类型<span
25                                                     class="tpl-form-line-small-title">Video type</span></label>
26                                             <div class="am-u-sm-9">
27                                                 <select data-am-selected="{searchBox: 1}" style="display: none;" name="type_video">
28                                                     <option value="a">入职培训</option>
29                                                     <option value="b">公共宣传</option>
30                                                     <option value="c">技能提升</option>
31                                                     <option value="d">其他</option>
32                                                 </select>
33 
34                                             </div>
35                                         </div>
36 
37                                         <div class="am-form-group">
38                                             <label class="am-u-sm-3 am-form-label">视频简介<span
39                                                     class="tpl-form-line-small-title">Video introduction</span></label>
40                                             <div class="am-u-sm-9">
41                                                 <input type="text"  name="introduction_video" placeholder="输入关键字">
42                                             </div>
43                                         </div>
44 
45                                         <div class="am-form-group">
46                                             <label for="user-weibo" class="am-u-sm-3 am-form-label">视频 <span
47                                                     class="tpl-form-line-small-title">video</span></label>
48                                         <div class="am-u-sm-9">
49                                                 
50                                                 <div id="content">
51                                                   
52                                                     <input type="file" name="video" id="filer_input2" multiple="multiple">
53                                                     
54                                                 </div>
55                                                 
56 
57                                             </div>
58                                         </div>
59                                         <!--     <div class="am-form-group">
60                                             <label for="user-weibo" class="am-u-sm-3 am-form-label">添加分类 <span
61                                                     class="tpl-form-line-small-title">Type</span></label>
62                                             <div class="am-u-sm-9">
63                                                 <input type="text" id="user-weibo" placeholder="请添加分类用点号隔开">
64                                                 <div>
65 
66                                                 </div>
67                                             </div>
68                                         </div> -->
69 
70 
71                                         <div class="am-form-group">
72                                             <label for="user-intro" class="am-u-sm-3 am-form-label">视频详细内容 <span
73                                                     class="tpl-form-line-small-title">Video details</span></label>
74                                             <div class="am-u-sm-9">
75                                                 <textarea  rows="10" id="user-intro"
76                                                     placeholder="请输入文章内容" name="details_video"></textarea>
77                                             </div>
78                                         </div>
79 
80                                         <div class="am-form-group">
81                                             <div class="am-u-sm-9 am-u-sm-push-3">
82                                                 <button type="submit"
83                                                     class="am-btn am-btn-primary tpl-btn-bg-color-success ">提交</button>
84                                             </div>
85                                         </div>
86                                     </form>

三、SpringMvc配置文件(别的配置文件就省略了)

<!-- 配置扫描器 -->
    <context:component-scan base-package="com.hts.controller" />
    
    <!-- 注解驱动:配置处理器映射器和适配器 -->
    <mvc:annotation-driven />
    
    <!--配置静态资源的访问映射,此配置中的文件,将不被前端控制器拦截 -->
    <mvc:resources location="/js/" mapping="/js/**" />
    <mvc:resources location="/css/" mapping="/css/**" />
    <mvc:resources location="/fonts/" mapping="/fonts/**" />
    <mvc:resources location="/images/" mapping="/images/**" />    
    
    <!-- 配置视图解释器ViewResolver -->
    <bean id="jspViewResolver" class=
    "org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>    
       
      <!-- 配置文件上传的视图解析器 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 默认编码 -->
        <property name="defaultEncoding" value="utf-8" />
        <!-- 文件大小最大值 -->
        <property name="maxUploadSize" value="10485760000" />
        <!-- 内存中的最大值 -->
        <property name="maxInMemorySize" value="40960" />
    </bean>

四、Po层,Dao层,Service层,Serviceimpl层,Controller层

public class Video {
    private Integer id;
    private String title_video;//视频标题
    private String introduction_video;//视频简介
    private String files;//视频文件
    private String details_video;//视频详细内容
    private String type_video;//视频类型
}
<!-- SQL -->
<insert id="insertSelective"
        parameterType="Video">
        insert into video
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="title_video != null">
                title_video,
            </if>
            <if test="introduction_video != null">
                introduction_video,
            </if>
            <if test="files != null">
                files,
            </if>
            <if test="details_video != null">
                details_video,
            </if>
            <if test="type_video != null">
                type_video,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="title_video != null">
                #{title_video,jdbcType=VARCHAR},
            </if>
            <if test="introduction_video != null">
                #{introduction_video,jdbcType=VARCHAR},
            </if>
            <if test="files != null">
                #{files,jdbcType=VARCHAR},
            </if>
            <if test="details_video != null">
                #{details_video,jdbcType=VARCHAR},
            </if>
            <if test="type_video != null">
                #{type_video,jdbcType=VARCHAR},
            </if>
            
        </trim>
    </insert>
//视频
public interface VideoDao {
    
    //上传视频
    
    int insertSelective(Video video);   

    
    
}
public interface VideoService {
    
    //上传视频
    
    int insertSelective(Video video);  
    
    

}
@Service("VideoService")
public class VideoServiceimpl implements VideoService {
    
    //自动装配
    @Autowired
    private VideoDao videoDao;


    @Override
    public int insertSelective(Video video) {
        // TODO Auto-generated method stub
        return videoDao.insertSelective(video);
        
        
    }
    
    

}
@Controller
public class VideoController {
    @Autowired
    private VideoService videoService;
    
    
    @RequestMapping("/upload.action")
    public String saveUpload(Video video,@RequestParam(value="video",required=false)MultipartFile multipartFile, HttpServletRequest request,HttpSession session) {
 
        // 以当前日期创建一个文件夹,避免单个文件夹中文件过多
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        // 截取年月日
        String substring = timestamp.toString().substring(0, 10);
        // 设置文件上传存放的路径
        String path="C:/Users/SJ002/Desktop/demo/htsWeb/src/main/webapp/upload/";
        String uploadPath = path + substring;
 
        System.out.println("获取到的文件上传地址为:" + uploadPath);
        // 获取上传文件名字
        String uploadName = multipartFile.getOriginalFilename();
        System.out.println("原始文件名:" + uploadName);
        // 利用UUID生成新的文件名字,避免原文件被覆盖
        String uuid ="HTS_"+ UUID.randomUUID().toString().replaceAll("-", "");
 
        // 截取上传文件的后缀
        String suffix = uploadName.substring(uploadName.lastIndexOf("."));
        // 拼接新的文件名字
        String newUploadName = uuid + suffix;
        System.out.println("新的文件名:" + newUploadName);
 
        File dir = new File(uploadPath, newUploadName);
        // exists() 测试此抽象路径名表示的文件或目录是否存在。
        if (!dir.exists()) {
            dir.mkdirs();
        }
        //MultipartFile自带的解析方法
        try {
            multipartFile.transferTo(dir);
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        
        video.setFiles(newUploadName);
        
        this.videoService.insertSelective(video);
        
        Everyone ua = (Everyone) session.getAttribute("ADM");
        int id=ua.getId();
        return "redirect:video_Post.action?id="+id;
 
    }

    
    

    


}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM