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