springboot接收別人上傳的本地視頻實例代碼
更新時間:2018年07月31日 11:39:01 作者:qq_33931552
本文通過實例代碼給大家介紹了springboot接收別人上傳的本地視頻,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧
package com.videobackend.controller; import java.io.File; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.springframework.web.multipart.MultipartFile; import com.ty.model.AdminTbl; import com.ty.model.RolePermissionTbl; import com.ty.service.PermissionService; import com.utils.Constants; import com.utils.ParamterNullCheck; import com.utils.ToInterface; import com.videobackend.model.Video; import com.videobackend.model.VideoRecommend; @Controller @RequestMapping(value = "/videoInfo") public class VideoController { /** * 調(diào)測日志記錄器。 */ private static final Logger DEBUGGER = Logger.getLogger(VideoController.class); @Autowired private PermissionService permissionService; private static String FILE_ADDRESS; @Value("${file_address}") public void setfILE_ADDRESS(String fILE_ADDRESS) { FILE_ADDRESS = fILE_ADDRESS; } /** * 上傳本地視頻 * * @param request * @param response * @param model * @param video * @param admintabl * @return * @throws IOException * @throws IllegalStateException */ @RequestMapping(value = "/save_local_video", method = RequestMethod.POST) @ResponseBody public String save_local_video(@RequestParam("file") MultipartFile file, Video video, AdminTbl admintabl) throws IllegalStateException, IOException { JSONObject result = new JSONObject(); String[] args = { "admin_id", "cover", "createdTime", "title"}; JSONObject nullcheck = ParamterNullCheck.getInstance().checkNull(video, args); if (!file.isEmpty()) { //存放地址 String path = FILE_ADDRESS; //如果父文件夾不存在 則創(chuàng)建文件夾 文件夾為path,視頻名字file.getOriginalFilename() File filepath = new File(path, file.getOriginalFilename()); if (!filepath.getParentFile().exists()) { filepath.getParentFile().mkdirs(); } File fi = new File(path + File.separator + file.getOriginalFilename()); //下載到本地 file.transferTo(fi); //獲取絕對路徑 String localAddress = fi.getAbsolutePath(); DEBUGGER.info("存入本地文件地址:" + localAddress); video.setLocalAddress(localAddress); //獲取后綴名 String suffix= localAddress.substring(localAddress.lastIndexOf("."), localAddress.length()); DEBUGGER.info("后綴名:" + suffix); video.setSuffix(suffix); if (nullcheck == null) { // 查詢該 用戶是否有該權(quán)限 admintabl.setUrl("/videoInfo/save_local_video"); RolePermissionTbl rpt = permissionService.get_permission(admintabl); if (rpt != null) { JSONObject param = null; param = (JSONObject) JSON.toJSON(video); DEBUGGER.info(param.toJSONString()); // 調(diào)取接口 StringBuffer saveLocalVideo = ToInterface.interfaceUtil("/video/saveLocalVideo", param.toJSONString(), "POST"); result.put("saveLocalVideo", saveLocalVideo); } else { result.put("msg", Constants.NO_AUTH); } } else { result = nullcheck; } } else { DEBUGGER.info("缺少的參數(shù)key=======" + file.getName()); result.put("msg", Constants.SYS_PARAMTER_MISSING); } return result.toJSONString(); } }
yml配置
#設(shè)置文件大小 srpingboot不設(shè)置會報錯 spring.servlet.multipart.max-file-size : 10mb spring.servlet.multipart.max-request-size : 100mb #文件存放地址 file_address: D:\\image
pom配置
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.2.2</version> </dependency>
總結(jié)
以上所述是小編給大家介紹的springboot接收別人上傳的本地視頻,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
關(guān)于Spring?@Transactional事務(wù)傳播機制詳解
我們?nèi)粘9ぷ髦袠O少使用事務(wù)傳播級別,單純只是使用事務(wù)和rollbackfor拋出異常來解決事務(wù)問題,但其實我們很多時候使用的是不正確的,或者說會造成事務(wù)粒度過大,本文詳解一下事務(wù)傳播級別,也讓自己更好地處理事務(wù)問題,需要的朋友可以參考下2023-08-08Struts2 文件上傳進(jìn)度條的實現(xiàn)實例代碼
本篇文章主要介紹了Struts2 文件上傳進(jìn)度條的實現(xiàn)實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05springboot post接口接受json時,轉(zhuǎn)換為對象時,屬性都為null的解決
這篇文章主要介紹了springboot post接口接受json時,轉(zhuǎn)換為對象時,屬性都為null的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10SpringBoot+Vue+Axios+BootStrap實現(xiàn)圖書的增刪改查功能示例
本文主要介紹了SpringBoot+Vue+Axios+BootStrap實現(xiàn)圖書的增刪改查功能,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12