java上传文件与文件夹到服务器
来源:互联网 发布:江西广电网络客服电话 编辑:程序博客网 时间:2024/06/05 01:14
文件:
String basePath = ServletActionContext.getServletContext().getRealPath("share"); //服务器路径获取 File savefile = new File(new File(basePath), uploadFileName); try { FileUtils.copyFile(upload, savefile); } catch (IOException e) { e.printStackTrace(); }
文件夹:
public String mutiUpload(){ if (folder == null) { path=path+""; return "mutiUpload"; } File mutiFile=new File(folder);//folder为上传的文件夹全路径 String parent=mutiFile.getParent();//你上传的文件夹的根路径 try { uploadMuti(folder,parent); } catch (IOException e) { e.printStackTrace(); } setSuccessNextUrl("window.location.href='contract/contractFtp!changeDir.action?path=" + path + "'"); return SUCCESS; } public boolean uploadMuti(String folder,String originPath) throws IOException{ File mutiFile=new File(folder); String basePath = ServletActionContext.getServletContext().getRealPath("share"); String folderName=(mutiFile.getPath().replace(originPath, "")).replace("\\", "/");//文件夹名称 String folderPath=basePath+"/"+folderName; File file=new File(folderPath); file.mkdir(); File fileList[] = mutiFile.listFiles(); for(File upfile : fileList){ if (upfile.isDirectory()) {// 文件夹中还有文件夹 folder=upfile.getPath(); uploadMuti(folder,originPath); }else{ String path=(upfile.getPath().replace(originPath, "")).replace("\\", "/"); String str[]=path.split("/"); String workpath=""; for(int i=0;i<str.length-1;i++){ workpath=workpath+"/"+str[i]; } String filePath=basePath+workpath; File savefile = new File(new File(filePath), upfile.getName()); try { FileUtils.copyFile(upfile, savefile); } catch (IOException e) { e.printStackTrace(); } } } return true; }
自己使用过后发现这种做法只能在自己搭建服务器的情况下从本机上传到服务器,而无法从其他机器上传。
0 0
- java上传文件与文件夹到服务器
- 文件上传到服务器某个文件夹
- Java文件上传到服务器
- java上传文件到服务器
- Java上传文件到服务器
- java上传文件到共享文件夹
- Java创建文件夹并上传文件到该文件夹
- Java创建文件夹并上传文件到该文件夹
- fileupload实现上传文件到服务器文件夹下
- xshell上传文件夹到服务器
- Java Client/Server 上传文件到服务器与保存文件到本地
- java FTP上传文件到服务器
- java servlet 上传文件到服务器
- java上传文件到FTP服务器
- java之TCP上传文件到服务器
- IOS上传文件到JAVA服务器
- java将文件上传到ftp服务器
- java 实现上传文件到远程服务器
- PF_NETLINK应用实例:NETLINK_KOBJECT_UEVENT的实现
- EChars显示labelLine;添加点击事件
- 使用Eclipse构建Maven项目 (step-by-step)
- Andriod 使用手机振动服务
- SQL Server2005远程连接方法
- java上传文件与文件夹到服务器
- 用Java程序把Word转换成Html文件
- Less实践第二篇
- JVM监控与调优
- 时间控件事件
- android WebView(四)与html交互
- 黑马程序员——OC基础语法----类、对象、方法
- 背包问题
- linux 下C调用Python 模块