文件夹内多文件上传到服务器
文件夾上傳至服務(wù)器
希望可以給你一點幫助
直接上代碼
jsp頁面
<div style="text-align:center; vertical-align:middel" id="uploadFile">
<input type="file" name="file" id="file" multiple="multiple"
webkitdirectory /> <br> <br>
<button id="upload" οnclick="f_upload()">上傳</button>
<button id="btnBack">返回</button>
</div>
1
2
3
4
5
6
7
function f_upload(){
var url =’<%=rootPath%>/module/impExp/ImpExpAction.do?method=dispatchImpt&code=<%=code%>’;
var paths = document.getElementById(“file”).files;
if(paths.length==0) {
alert(“請選擇文件”);
return;
}
var formData=new FormData();
for(var i = 0;i<paths.length;i++){
var file=paths[i];
formData.append(paths[i].name,paths[i]);
}
$.ajax({
url : url,
cache : false,
data : formData,
type : ‘POST’,
processData : false,
contentType : false,
success : function(flag) {
if (flag != “0”) {
alert(“上傳成功”);
javascript: history.back(-1);
}else{
alert(“上傳失敗!!!”)
}
}
});
}
//返回
function f_back() {
javascript: history.back(-1);
}
后端代碼
public ActionForward dispatchImpt(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
/*
* 方法二 :
*/
ResourceBundle rb = ResourceBundle.getBundle(“config”);
String userDataHomeDir = rb.getString(“USER_DATA_HOME_DIR”) + rb.getString(“AFFIX_DIR”);
String oss = commonUtil.getCloudPath();
String codeNumber =commonUtil.getCodeNumber();
>
String code = request.getParameter("code")==null?"":request.getParameter("code");
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
request.setCharacterEncoding("utf-8");
MultipartHttpServletRequest multiRequest = multipartResolver.resolveMultipart(request);
//取得request中的所有文件名
Iterator<String> iter = multiRequest.getFileNames();
while(iter.hasNext()){
//取得上傳文件
MultipartFile file = multiRequest.getFile(iter.next());
if(file!=null){
//取得當前上傳文件的文件名稱
String myFileName = file.getOriginalFilename();
//如果名稱不為“”,說明該文件存在,否則說明該文件不存在
if(myFileName.trim()!=""){
//重命名上傳后的文件名
String fileName = file.getOriginalFilename();
//定義文件上傳路徑
String dirPath=userDataHomeDir+codeNumber+code;
//阿里云存放地址
String ossPath = oss+codeNumber+code+"/"+fileName.substring(0,fileName.lastIndexOf("."))+"/"+fileName;
File dir = new File(dirPath);
if(!dir.exists()){
dir.mkdirs();
}
File localFile = new File(dir,"/"+fileName);
file.transferTo(localFile);
String uploadpath =dirPath+"/"+fileName;
System.out.println("ossPath======="+ossPath);
System.out.println("localFile==="+localFile);
System.out.println("阿里云存放路徑==="+uploadpath);
}
}
}
CommonUtil.deleteDir(bd.toString());
return null;
}
---------------------?
轉(zhuǎn)載于:https://www.cnblogs.com/hyhy904/p/10958358.html
總結(jié)
以上是生活随笔為你收集整理的文件夹内多文件上传到服务器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2019攻防世界web新手区
- 下一篇: KTMfuke390和RC390后避震一