當前位置:首頁 » 文件管理 » spring上傳進度

spring上傳進度

發布時間: 2022-06-24 10:14:10

『壹』 springmvc怎麼上傳文件

springmvc怎麼實現文件上傳

package com.test.controller;

import java.io.File;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
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 org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.test.servlet.NoSupportExtensionException;
import com.test.servlet.State;

@Controller
@RequestMapping(value = "/mvc")
public class UploadController {

/** 日誌對象*/
private Log logger = LogFactory.getLog(this.getClass());

private static final long serialVersionUID = 1L;

/** 上傳目錄名*/
private static final String uploadFolderName = "uploadFiles";

/** 允許上傳的擴展名*/
private static final String [] extensionPermit = {"txt", "xls", "zip"};

@RequestMapping(value = "/upload.do", method = RequestMethod.POST)
public @ResponseBody Map<String, Object> fileUpload(@RequestParam("file") CommonsMultipartFile file,
HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception{
logger.info("UploadController#fileUpload() start");

//清除上次上傳進度信息
String curProjectPath = session.getServletContext().getRealPath("/");
String saveDirectoryPath = curProjectPath + "/" + uploadFolderName;
File saveDirectory = new File(saveDirectoryPath);
logger.debug("Project real path [" + saveDirectory.getAbsolutePath() + "]");

// 判斷文件是否存在
if (!file.isEmpty()) {
String fileName = file.getOriginalFilename();
String fileExtension = FilenameUtils.getExtension(fileName);
if(!ArrayUtils.contains(extensionPermit, fileExtension)) {
throw new NoSupportExtensionException("No Support extension.");
}
file.transferTo(new File(saveDirectory, fileName));
}

logger.info("UploadController#fileUpload() end");
return State.OK.toMap();
}

}

『貳』 基於springmvc文件上傳並顯示進度條前台是怎麼獲取session

前台獲取session???現在的html是靜態語言不能獲取的吧,又不像jsp那樣直接request.getSession。並且b端獲取session根本沒啥意義

『叄』 SpringBoot超大文件上傳如何實現

不管什麼技術,超大文件上傳(超出一次tcp上限)都是要做分片和合並的,無非是自己做還是找控制項的差別。
另外,springboot是後台接收,前端實現是由前端框架負責,比如vue。
以下是Vue+Springboot實現大文件上傳的二種方式:
1、利用ElementUI的el-upload
優點:
簡單方便,可以實現功能
缺點:
上傳速度太慢,沒有分片單線程上傳1個G的文件即使在區域網也很慢
上傳顯示的進度條不準確,進度已經100%了,但是還需要等很久在服務端才生成完文
2、利用網路的webuploader
優點:
WebUploader是網上比較推薦的方式,分片上傳大文件速度很快。
缺點:
必須依賴 jquery
不能 import 導入,只能在 index.html 里包含。
3. 利用vue-uploader
vue-uploader 是基於vue的uploader組件,預設就是分片上傳。
通過npm安裝,基本流程參考github上的說明即可。
上傳的基本原理就是前端根據文件大小,按塊大小分成很多塊,然後多線程同時上傳多個塊,同時調用服務端的上傳介面,服務端會生成很多小塊小塊的文件。
所有塊都上傳完之後,前端再調用一個服務端的merge介面,服務端把前面收到的所有塊文件按順序組合成最終的文件。

『肆』 如何實現Spring MVC 圖片上傳時,圖片如何代替進度條

不是用圖片代替進度條吧,應該可以用個div加點樣式,根據進度調整寬度呀。哈哈 個人愚見。

『伍』 spring文件上傳

萬變不離其宗,要實現文件的上傳需要對應的JAR包:
1、commons-fileupload-1.2.2.jar
2、commons-io-2.0.1.jar

『陸』 springmvc怎麼做上傳進度條,前台後台分別怎麼寫

前台一般傳標準的時間格式的字元串到後台,後台接收到時間字元串後用java.lang.util包下的SimpleDateFormat解析成date類型即可。

『柒』 java多文件上傳顯示進度條

使用 apache fileupload ,spring MVC jquery1.6x , bootstrap 實現一個帶進度條的多文件上傳,由於fileupload 的局限,暫不能實現每個上傳文件都顯示進度條,只能實現一個總的進度條,效果如圖:

packagecom.controller;

importjava.util.List;

importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;

importorg.apache.commons.fileupload.FileItemFactory;
importorg.apache.commons.fileupload.ProgressListener;
importorg.apache.commons.fileupload.disk.DiskFileItemFactory;
importorg.apache.commons.fileupload.servlet.ServletFileUpload;
importorg.apache.log4j.Logger;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestMethod;
importorg.springframework.web.bind.annotation.ResponseBody;
importorg.springframework.web.servlet.ModelAndView;

@Controller
{
Loggerlog=Logger.getLogger(FileUploadController.class);

/**
*upload上傳文件
*@paramrequest
*@paramresponse
*@return
*@throwsException
*/
@RequestMapping(value="/upload.html",method=RequestMethod.POST)
publicModelAndViewupload(HttpServletRequestrequest,
HttpServletResponseresponse)throwsException{
finalHttpSessionhs=request.getSession();
ModelAndViewmv=newModelAndView();
booleanisMultipart=ServletFileUpload.isMultipartContent(request);
if(!isMultipart){
returnmv;
}
//Createafactoryfordisk-basedfileitems
FileItemFactoryfactory=newDiskFileItemFactory();

//Createanewfileuploadhandler
ServletFileUploapload=newServletFileUpload(factory);
upload.setProgressListener(newProgressListener(){
publicvoipdate(longpBytesRead,longpContentLength,intpItems){
ProcessInfopri=newProcessInfo();
pri.itemNum=pItems;
pri.readSize=pBytesRead;
pri.totalSize=pContentLength;
pri.show=pBytesRead+"/"+pContentLength+"byte";
pri.rate=Math.round(newFloat(pBytesRead)/newFloat(pContentLength)*100);
hs.setAttribute("proInfo",pri);
}
});
Listitems=upload.parseRequest(request);
//Parsetherequest
//Processtheuploadeditems
//Iteratoriter=items.iterator();
//while(iter.hasNext()){
//FileItemitem=(FileItem)iter.next();
//if(item.isFormField()){
//Stringname=item.getFieldName();
//Stringvalue=item.getString();
//System.out.println("thisiscommonfeild!"+name+"="+value);
//}else{
//System.out.println("thisisfilefeild!");
//StringfieldName=item.getFieldName();
//StringfileName=item.getName();
//StringcontentType=item.getContentType();
//booleanisInMemory=item.isInMemory();
//longsizeInBytes=item.getSize();
//FileuploadedFile=newFile("c://"+fileName);
//item.write(uploadedFile);
//}
//}
returnmv;
}


/**
*process獲取進度
*@paramrequest
*@paramresponse
*@return
*@throwsException
*/
@RequestMapping(value="/process.json",method=RequestMethod.GET)
@ResponseBody
publicObjectprocess(HttpServletRequestrequest,
HttpServletResponseresponse)throwsException{
return(ProcessInfo)request.getSession().getAttribute("proInfo");
}

classProcessInfo{
publiclongtotalSize=1;
publiclongreadSize=0;
publicStringshow="";
publicintitemNum=0;
publicintrate=0;
}

}

『捌』 前端上傳文件實時顯示進度條和上傳速度的工作原理是怎樣的

後端的責任。

『玖』 spring mvc 怎麼實現上傳文件

springmvc文件上傳
1.加入jar包:
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
lperson.java中加屬性,實現get ,set方法
private String photoPath;
2.創建WebRoot/upload目錄,存放上傳的文件

1 <sf:form id="p" action="saveOrUpdate"
2 method="post"
3 modelAttribute="person"
4 enctype="multipart/form-data">
5
6 <sf:hidden path="id"/>
7 name: <sf:input path="name"/><br>
8 age: <sf:input path="age"/><br>
9 photo: <input type="file" name="photo"/><br>

上面第9行文件上傳框,不能和實體對象屬性同名,類型不同

controller配置

1 12、文件上傳功能實現 配置文件上傳解析器
2 @RequestMapping(value={"/saveOrUpdate"},method=RequestMethod.POST)
3 public String saveOrUpdate(Person p,
4 @RequestParam("photo") MultipartFile file,
5 HttpServletRequest request
6 ) throws IOException{
7 if(!file.isEmpty()){
8 ServletContext sc = request.getSession().getServletContext();
9 String dir = sc.getRealPath(「/upload」); //設定文件保存的目錄
10
11 String filename = file.getOriginalFilename(); //得到上傳時的文件名
12 FileUtils.writeByteArrayToFile(new File(dir,filename), file.getBytes());
13
14 p.setPhotoPath(「/upload/」+filename); //設置圖片所在路徑
15
16 System.out.println("upload over. "+ filename);
17 }
18 ps.saveOrUpdate(p);
19 return "redirect:/person/list.action"; //重定向
20 }

3.文件上傳功能實現 spring-mvc.xml 配置文件上傳解析器

1 <!-- 文件上傳解析器 id 必須為multipartResolver -->
2 <bean id="multipartResolver"
3 class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
4 <property name="maxUploadSize" value=「10485760"/>
5 </bean>
6
7 maxUploadSize以位元組為單位:10485760 =10M id名稱必須這樣寫

1 映射資源目錄
2 <mvc:resources location="/upload/" mapping="/upload/**"/>

隨即文件名常用的三種方式:
文件上傳功能(增強:防止文件重名覆蓋)
fileName = UUID.randomUUID().toString() + extName;
fileName = System.nanoTime() + extName;
fileName = System.currentTimeMillis() + extName;

1 if(!file.isEmpty()){
2 ServletContext sc = request.getSession().getServletContext();
3 String dir = sc.getRealPath("/upload");
4 String filename = file.getOriginalFilename();
5
6
7 long _lTime = System.nanoTime();
8 String _ext = filename.substring(filename.lastIndexOf("."));
9 filename = _lTime + _ext;
10
11 FileUtils.writeByteArrayToFile(new File(dir,filename), file.getBytes());
12
13 p.setPhotoPath("/upload/"+filename);
14
15 System.out.println("upload over. "+ filename);
16 }

圖片顯示 personList.jsp
1 <td><img src="${pageContext.request.contextPath}${p.photoPath}">${p.photoPath}</td>

『拾』 如何在spring mvc 里加入監聽器,來監聽文件上傳進度條

一般要用第三方 上傳的 軟體。有flash支持的那種
否則進度條實現還是比較復雜的
要新建一個線程 不停的查看伺服器端已經接收到的文件的大小
然後用這個大小 除以 上傳文件的本來size 才能得出一個百分比。

熱點內容
楚雄琪豐酒店wifi密碼多少 發布:2024-03-29 23:20:10 瀏覽:509
自招編程題 發布:2024-03-29 23:19:31 瀏覽:192
蘋果端的什麼游戲與安卓數據互通 發布:2024-03-29 23:18:23 瀏覽:695
androidwear表盤 發布:2024-03-29 23:09:46 瀏覽:833
19萬的紅旗有哪些配置 發布:2024-03-29 23:09:44 瀏覽:985
裝修公司網站源碼 發布:2024-03-29 23:01:45 瀏覽:454
安卓手機哪個有nfc功能 發布:2024-03-29 22:59:25 瀏覽:554
newifi搭建伺服器 發布:2024-03-29 22:56:43 瀏覽:957
神演算法 發布:2024-03-29 22:38:54 瀏覽:106
教學視頻文字腳本 發布:2024-03-29 22:29:49 瀏覽:138