Folder file upload to the server

Folder uploaded to the server
hope can give you a little help

Directly on the code
jsp page

<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" onclick="f_upload()">上传</button>
&nbsp;&nbsp;
<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");
the CommonsMultipartResolver the MultipartResolver the CommonsMultipartResolver = new new ();
request.setCharacterEncoding ( "UTF-8");
a MultipartHttpServletRequest multiRequest = multipartResolver.resolveMultipart (request);
// get all the files request in the name of
Iterator <String> iter = multiRequest.getFileNames ();
the while (iter.hasNext ()) {
// upload the file
MultipartFile file = multiRequest.getFile (iter .next ());
! IF (file = null) {
// get the current uploaded file name
String myfilename = file.getOriginalFilename ();
// If the name is not "", indicating that the file exists, otherwise the file description there is no
IF (myFileName.trim ()! = "") {
// rename the file name upload
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;
}
--------------------- 

Guess you like

Origin www.cnblogs.com/hyhy904/p/10958358.html