文件夹上传至服务器
希望可以给你一点帮助

直接上代码
jsp页面

<div style="text-align:center; vertical-align:middel" >返回</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;
}
--------------------- 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-03
  • 2021-12-14
  • 2022-12-23
猜你喜欢
  • 2021-08-11
  • 2022-12-23
  • 2022-12-23
  • 2022-01-17
  • 2021-11-29
  • 2021-08-11
相关资源
相似解决方案