Java實現(xiàn)文件上傳保存
本文實例為大家分享了Java實現(xiàn)文件上傳保存的具體代碼,供大家參考,具體內(nèi)容如下
框架
Spring Boot + FreeMarker + Ajax
第一次嘗試Spring Boot 和FreeMarker, Spring Boot最大的感受是真的方便,約定大于配置,很多東西都是在使用過程中了解,看Spring Boot實戰(zhàn)基本看完就忘得差不多…
后臺
1. 從request獲取到文件
MultipartFile類保存文件信息,文件上傳放在request中,可debug查看request中實體觀察到。
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
List<MultipartFile> files = multipartRequest.getFiles("img");強制轉(zhuǎn)換request獲取MultipartHttpServletRequest,再獲得MultipartFile文件
2. 寫入上傳文件夾
path在application.properties中定義,使用@Value標簽注入。
/**
? ? ?* 保存圖像
? ? ?*
? ? ?* @param img
? ? ?* @param userName
? ? ?* @return
? ? ?*/
? ? private String writeImgToUpload(MultipartFile img, String userName) {
? ? ? ? // Tomcat 放在C盤中,可能無讀寫權限而寫入失敗
? ? ? ? // 寫入目錄文件
? ? ? ? // 獲取文件格式
? ? ? ? String suffix = img.getOriginalFilename().substring(img.getOriginalFilename().lastIndexOf("."));
? ? ? ? // 目標文件路徑+文件名
? ? ? ? String imgFile = path + userName + suffix;
? ? ? ? File toFile = new File(imgFile);
? ? ? ? if (!toFile.getParentFile().exists()) {
? ? ? ? ? ? // when file is not existed, will create.
? ? ? ? ? ? toFile.mkdirs();
? ? ? ? }
? ? ? ? // write to target file.
? ? ? ? try {
? ? ? ? ? ? img.transferTo(toFile);
? ? ? ? ? ? return imgFile;
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? return null;
? ? }前端
<form class="form-horizontal" id="authForm" method="post" enctype="multipart/form-data"> ? ? <div class="form-group"> ? ? ? ? <div class="col-sm-6"> ? ? ? ? ? ? <b><label for="img">上傳認證</label></b> ? ? ? ? ? ? <input type="file" id="img" name="img"> ? ? ? ? </div> ? ? </div> ? ? <div class="row"> ? ? ? ? <div class="col-xs-4"> ? ? ? ? ? ? <button type="submit" class="btn btn-primary btn-block btn-flat">注冊</button> ? ? ? ? </div> ? ? </div> </form>
Ajax通信
submitHandler : function(form) {
? ? ? ? ? ? var file = new FormData();
? ? ? ? ? ? var img = $('#img')[0].files[0];
? ? ? ? ? ? file.append('img', img);
? ? ? ? ? ? $.ajax({
? ? ? ? ? ? ? ? url:base_url + "/auth/update",
? ? ? ? ? ? ? ? type: "POST",
? ? ? ? ? ? ? ? data: file,
? ? ? ? ? ? ? ? async: false,
? ? ? ? ? ? ? ? cache: false,
? ? ? ? ? ? ? ? contentType: false,
? ? ? ? ? ? ? ? processData: false,
? ? ? ? ? ? ? ? success: function (data) {
? ? ? ? ? ? ? ? ? ? // 成功的回調(diào)
? ? ? ? ? ? ? ? ? ? if (data.code == 200) {
? ? ? ? ? ? ? ? ? ? ? ? layer.msg('認證成功');
? ? ? ? ? ? ? ? ? ? ? ? setTimeout(function () {
? ? ? ? ? ? ? ? ? ? ? ? ? ? window.location.href = base_url;
? ? ? ? ? ? ? ? ? ? ? ? }, 500);
? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? layer.open({
? ? ? ? ? ? ? ? ? ? ? ? ? ? icon: '2',
? ? ? ? ? ? ? ? ? ? ? ? ? ? content: (data.msg || '認證失敗')
? ? ? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? });以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
java本機內(nèi)存分配Native?memory?allocation?mmap失敗問題解決
這篇文章主要介紹了java本機內(nèi)存分配Native?memory?allocation?mmap失敗問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11
SpringBoot?2.5.5整合輕量級的分布式日志標記追蹤神器TLog的詳細過程
分布式追蹤系統(tǒng)是一個最終的解決方案,如果您的公司已經(jīng)上了分布式追蹤系統(tǒng),這篇文章主要介紹了SpringBoot?2.5.5整合輕量級的分布式日志標記追蹤神器TLog,需要的朋友可以參考下2022-10-10

