1. 程式人生 > >struts2基於模型驅動的檔案上傳(上傳視訊為例子)

struts2基於模型驅動的檔案上傳(上傳視訊為例子)

package cn.hbmy.tas.entity;

import java.io.File;

public class Video {
	private Integer id;     //id
	private File video;
	private String videoFileName;   //表示  上傳的視訊的資料名字
	private String videoContentType;   //檔案的字尾名
	private User user;     //上傳的人相關聯	起來
	private String  savePath;   //表示儲存的路徑
	
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public File getVideo() {
		return video;
	}
	public void setVideo(File video) {
		this.video = video;
	}
	public String getVideoFileName() {
		return videoFileName;
	}
	public void setVideoFileName(String videoFileName) {
		this.videoFileName = videoFileName;
	}
	public String getVideoContentType() {
		return videoContentType;
	}
	public void setVideoContentType(String videoContentType) {
		this.videoContentType = videoContentType;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public String getSavePath() {
		return savePath;
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
}

這是實體類的建立

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!-- 引入js標籤庫 -->
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>上傳視訊</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script language="javascript" src="${pageContext.request.contextPath}/script/jquery.js"></script>
    <script language="javascript" src="${pageContext.request.contextPath}/script/pageCommon.js" charset="utf-8"></script>
    <script language="javascript" src="${pageContext.request.contextPath}/script/PageUtils.js" charset="utf-8"></script>
    <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/style/blue/pageCommon.css" />
    <script type="text/javascript">
    </script>
</head>
<body>
<s:form action="video_add.action" enctype="multipart/form-data" method="post">
	請選擇視訊檔案:<s:file name="video"></s:file><br/>
	<s:submit value="上傳"></s:submit>
	<s:fielderror></s:fielderror>
</s:form>
</body>
</html>
上面就是上傳頁面的jsp檔案,注意表單的提交!
public String add() throws Exception {
		// 封裝帶物件中 儲存資料庫 有時候 我們也可以使用model 但是要設定為封裝的屬性
		//得到檔案的真實的路徑
		//檔案上傳成功了;
		ServletContext application = ServletActionContext.getServletContext();
		String directory = application.getInitParameter("rootDirectory");
		String saveFilePath = directory;
	//	File file = new File(saveFilePath+model.getUser().getUserName());
		File file = new File(saveFilePath);
	    // 如果指定的路徑沒有就建立  
	    if (!file.exists()) {  
	         file.mkdirs();  
	    }  
		File target = new File(saveFilePath,model.getVideoFileName());
		FileUtils.copyFile(model.getVideo(),target);
		model.setSavePath(saveFilePath);   
		//這樣我們就儲存了   資料的路徑  我們查詢的時候,根據這個路徑   找到對應視訊資源到的id,從而就可以實現下載的功能了
		videoService.save(model);
		return "toList";
	}
上面就是VideoAction類裡面的add()新增視訊的方法
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC 
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd" >

<struts>
	<!-- 指定國際化資原始檔的baseName為uploadFile -->
        <constant name="struts.custom.i18n.resources" value="uploadFile"></constant>
	<!-- 配置為開發模式 -->
	<constant name="struts.devMode" value="true"/>
	<!-- 配置編碼 -->
	<constant name="struts.i18n.encoding" value="UTF-8"/>
	<!-- 把副檔名配置為action -->
	<constant value="action" name="struts.action.extension"/>
	<!-- 把主題配置為simple -->
	<constant value="simple" name="struts.ui.theme"/>
   <!-- 設定檔案上傳的大小  視訊檔案上傳最大為100M-->
	<constant name="struts.multipart.maxSize" value="104857600"></constant>
	<!-- 指定視訊上傳的型別, -->
		<package name="demo" extends="struts-default" namespace="/">
		<action name="video_*" class="videoAction" method="{1}">
		<!-- 我們在這兒配置攔截器,只允許視訊字尾名的檔案才能夠上傳 -->
			<interceptor-ref name="defaultStack">
				<param name="fileUpload.allowedExtensions">avi,rmvb,rm,asf,divx,mpg,mpeg,mpe,wmv,mp4,mkv,vob</param>
			</interceptor-ref>
			<result name="list">/System_Video/list.jsp</result>
			<result name="addUI">/System_Video/addUI.jsp</result>
			<result name="input">/System_Video/addUI.jsp</result>
			<result name="toList" type="redirectAction">video_list</result>
		</action>
	</package>
</struts>
上面的就是struts.xml的檔案中的配置,都有解釋,大家參考一下。
 <!--顯示資料列表-->
       <s:iterator value="videoList">
            <tr class="TableDetail1 template">
                <td>${videoFileName} </td>
                <td>${user.userName} </td>
                <td>
                    <s:a action="user_editUI?id=%{id}">播放</s:a>
					
					<s:a action="user_initPassword?id=%{id}" onclick="return window.confirm('您確定要下載嗎?')">下載</s:a>
                
                	<s:a action="video_delete?id=%{id}" onclick="return delConfirm()">刪除</s:a>
                </td>
            </tr>
       </s:iterator>
上面的就是顯示的jsp頁面,由於我在寫一個管理系統,這個裡面的程式碼量比較的大,我就貼上部分核心的程式碼!

struts.messages.error.file.too.large=\u4E0A\u4F20\u6587\u4EF6{1}\u8FC7\u5927
struts.messages.error.content.type.not.allowed=\u4E0A\u4F20\u7684\u6587\u4EF6\u53EA\u80FD\u662F\u89C6\u9891\u6587\u4EF6
struts.messages.error.uploading=\u4E0A\u4F20\u8FC7\u7A0B\u4E2D\u51FA\u73B0\u672A\u77E5\u7684\u9519\u8BEF

這一部分就是設定檔案上傳時候出現的錯誤的返回的資訊!

這個程式碼不全,僅供大家參考!後面我會將視訊的線上播放以及下載發出來。大家有什麼建議也可以給我說,畢竟我是第一次自己獨立的寫一個小型的專案!

因為好多人問我,我就把程式碼分享出來,大家一起學習!

連結:https://pan.baidu.com/s/1a5LkDC3T8-5xuU-fHjR3qQ 密碼:w726

相關推薦

struts2基於模型驅動檔案視訊例子

package cn.hbmy.tas.entity; import java.io.File; public class Video { private Integer id; //id private File video; private String

資料庫sql互轉oracle轉mysql例子

轉自: https://blog.csdn.net/sinat_32366329/article/details/76402059   在PowerDesinger裡找到 File -->> Reverse Engineer --->> Database  

audio-基於Vue的一個小專案音樂連結

1 標籤定義聲音,比如音樂或其他音訊流。 2 設定為自動播放的 audio 元素:autoplay=“autoplay” 但是隻有pc端可以實現 移動端不行(pc端的瀏覽器要比移動端的完善很多,對有些屬性支援也會好很多) 3 audio不單單是個標籤 他也是window下的一個物件,物件

ubuntu下使用filezilla檔案許可權問題open for write: permission denied

今天在使用filezilla連線虛擬機器中的ubuntu的時候出現上次出錯,錯誤詳情為: open for write: permission denied 看完錯誤大概知道和許可權有問題,

讓JSON請求和引數請求一樣,可以通過Struts2模型驅動給Action的類成員變數賦值欄位驅動模型驅動

自己定義一個攔截器: import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.

SpringMVC中文件的到服務器和下載問題--------下載

cat exc stream log trac close pri page fin 一、建立一個簡單的jsp頁面。 我們在建好的jsp的頁面中加入一個超鏈接:<a href="${pageContext.request.contextPath}/down

模型驅動的深度學習ADMM-net

for 高精 高精度 不同 height 梯度 深度學習 減少 需求 流程:模型族->算法族->深度網絡->深度學習 模型族:模型中含有超參數,給予不同的參數對應不同的模型,就形成了模型族 算法族:每一個模型對應一個完整算法,整個模型族對應了一個算法族 將

【轉載】基於rasa的對話系統搭建

生成模型 efi 實體類 total ted twisted -m serve feature 文章介紹使用rasa nlu和 rasa core 實現一個電信領域對話系統demo,實現簡單的業務查詢辦理功能,更完善的實現需要

Android 資料圖片的時候同時圖片名,拍照時間之類的

// 上傳檔案public void upload(File file) {try {// post請求需要攜帶的引數AjaxParams params = new AjaxParams();// 將檔案放入引數中LoginInfo loginInfo = new LoginInfo();login

Spark2.1.0模型設計與基本架構

  隨著近十年網際網路的迅猛發展,越來越多的人融入了網際網路——利用搜索引擎查詢詞條或問題;社交圈子從現實搬到了Facebook、Twitter、微信等社交平臺上;女孩子們現在少了逛街,多了在各大電商平臺上的購買;喜歡棋牌的人能夠在對戰平臺上找到世界各地的玩家對弈。在國內隨著網民數量的持續增加,造成網際網路公

Struts2模型驅動方式封裝資料

主要解決的問題:是在action中如果獲取請求引數 主要有兩種方式: 屬性驅動 直接在action類中提供與請求引數匹配屬性,提供get/set方法 在action類中創始一個javaBean,對其提供get/set ,在請求時頁面上要進行修改        

第一次使用Git本地專案到github,下載、安裝、很詳細,很全面

我覺得學習github的人基本上已經快要脫離了小白的標籤,雖然我一直喜歡自稱為小白。對於程式設計師原來說應該都聽說過GitHub,GitHub有許多開源的的專案和一些前沿的技術。因為自己在第一次使用過Git和github時遇到過不少的坑,所以,想對第一次使用Gi

TP5中含圖片的表單ajax不含圖片回顯

前端:<div class="page-container"> <form action="" method="post" class="form form-horizontal" id="formadd"> <div class="row

Struts2模型驅動(ModelDriven)

1.  模型驅動:           模型驅動是使用javaBean物件來封裝請求引數,在整個MVC流程中可以直接使用這個物件。在傳送請求後Struts將各個請求引數(javab的屬性)封裝到一個JavaBean物件中,Action接收這個例項物件並用該物件進行相關處理返回處理結果。          

5-11 基於詞頻的檔案相似度30分

5-11 基於詞頻的檔案相似度 (30分) 實現一種簡單原始的檔案相似度計算,即以兩檔案的公共詞彙佔總詞彙的比例來定義相似度。為簡化問題,這裡不考慮中文(因為分詞太難了),只考慮長度不小於3、且不超過10的英文單詞,長度超過10的只考慮前10個字母。 輸

基於servlet併發的日誌儲存

由於servlet本身就是併發處理的,因此我們只需要處理好對應的呼叫函式就可以了。 考慮到對效率有要求,因此使用一個StringBuffer做緩衝區,將資料儲存在記憶體中,然後定時將資料寫入檔案。基本思路是先將檔名儲存在記憶體中,將url的filename

mui開發app之多圖壓縮與仿qq空間說說發表

應廣大讀者建議,已經將該專案原始碼提交到地址: https://download.csdn.net/download/u014466109/10465677 與本部落格相關的多圖壓縮上傳程式碼在dashen/service/ask.html,請解壓專案並移動

Struts2 ModelDriven(模型驅動)注意事項

以下僅作為個人筆記記錄,如有不當,多謝指出 首先,Action必須實現ModelDriven介面,可在implements ModelDriven<T> T中指定泛型,即你需要用到的Model類,如:要登入,從登入頁面提交上來userName和password,

【SSH快速進階】——struts2模型驅動—ModelDriven

上篇部落格《SSH快速進階——struts2簡單的例項》中,處理使用者登陸的action—LoginAction為: package com.danny.user.action; public cl

細說linux IPC基於socket的程序間通訊

    【版權宣告:尊重原創,轉載請保留出處:blog.csdn.net/shallnet 或 .../gentleliu,文章僅供學習交流,請勿用於商業用途】     在一個較大的工程當中,一般都會有多個程序構成,各個功能是一個獨立的程序在執行。既然多個程序構成一個工程,