
代码ftp上传下载
21 上传代码:
import javaioFile;
import javaioFileInputStream;
import orgapachecommonsnetftpFTPClient;
import orgapachecommonsnetftpFTPReply;
public class test {
private FTPClient ftp;
/
@param path 上传到ftp服务器哪个路径下
@param addr 地址
@param port 端口号
@param username 用户名
@param password 密码
@return
@throws Exception
/
private boolean connect(String path,String addr,int port,String username,String password) throws Exception {
boolean result = false;
ftp = new FTPClient();
int reply;
ftpconnect(addr,port);
ftplogin(username,password);
ftpsetFileType(FTPClientBINARY_FILE_TYPE);
reply = ftpgetReplyCode();
if (!FTPReplyisPositiveCompletion(reply)) {
ftpdisconnect();
return result;
}
ftpchangeWorkingDirectory(path);
result = true;
return result;
}
/
@param file 上传的文件或文件夹
@throws Exception
/
private void upload(File file) throws Exception{
if(fileisDirectory()){
ftpmakeDirectory(filegetName());
ftpchangeWorkingDirectory(filegetName());
String[] files = filelist();
for (int i = 0; i < fileslength; i++) {
File file1 = new File(filegetPath()+"\\"+files[i] );
if(file1isDirectory()){
upload(file1);
ftpchangeToParentDirectory();
}else{
File file2 = new File(filegetPath()+"\\"+files[i]);
FileInputStream input = new FileInputStream(file2);
ftpstoreFile(file2getName(), input);
inputclose();
}
}
}else{
File file2 = new File(filegetPath());
FileInputStream input = new FileInputStream(file2);
ftpstoreFile(file2getName(), input);
inputclose();
}
}
public static void main(String[] args) throws Exception{
test t = new test();
tconnect("", "localhost", 21, "yhh", "yhhazr");
File file = new File("e:\\uploadify");
tupload(file);
}
}
22 下载代码
这里没有用到filter,如果用filter就可以过滤想要的文件。
public class Ftp { / @param args / public static void main(String[] args) { // TODO Auto-generated method stub Ftp ftp = new Ftp(); String hostname = ">改成<add key="ConnectionString" value="~/App_Data/jmzTeaDBmdb"/>
注意路径,改成你自己的,一般mdb都是放在app_data下的。~在net中是代表网站根目录
然后在连接的时候字符串是
string db = @"Provider=MicrosoftJetOleDb40;Data Source=" + >1、Source files:源目录。设置的是相对路径(注意:不是主目录的项目路径,而是视图的相对路径)。
jar包所在全路径为 /root/jenkins/workspace/gateway/sc-cloud/gateway/target/gateway-10-SNAPSHOTjar,
其中/root/jenkins为主目录,/workspace/gateway为所建视图位置,
还有一个坑,最终的配置前面不需要加斜杠了/
最后要填的是 sc-cloud/gateway/target/gateway-10-SNAPSHOTjar
2、Remove prefix:要去掉的前缀
3、Remote directory:要复制到的远程目录(相对路径)。在设置远程服务器时已经设置了目录 /test001,此处填 /builds 所以最终要复制到的目录是 /test001/builds
一般在处理Web应用下的文件创建与移动等时,会涉及到很多关于java
中相对路径,绝对路径等问题。以下是一些总结。希望大家遇到类似的问题,可以更有效的解决。
=================================================================================
1基本概念的理解
绝对路径:绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如:
C:\xyz\testtxt 代表了testtxt文件的绝对路径。>package orgchengfile;
import javaioFile;
public class FileTest {
public static void main(String[] args) throws Exception {
Systemoutprintln(ThreadcurrentThread()getContextClassLoader()getResource(""));
Systemoutprintln(FileTestclassgetClassLoader()getResource(""));
Systemoutprintln(ClassLoadergetSystemResource(""));
Systemoutprintln(FileTestclassgetResource(""));
Systemoutprintln(FileTestclassgetResource("/")); //Class文件所在路径
Systemoutprintln(new File("/")getAbsolutePath());
Systemoutprintln(SystemgetProperty("userdir"));
}
}
42服务器中的Java类获得当前路径(来自网络)
(1)Weblogic
WebApplication的系统文件根目录是你的weblogic安装所在根目录。
例如:如果你的weblogic安装在c:\bea\weblogic700
那么,你的文件根路径就是c:\
所以,有两种方式能够让你访问你的服务器端的文件:
a使用绝对路径:
比如将你的参数文件放在c:\yourconfig\yourconfproperties,
直接使用 new FileInputStream("yourconfig/yourconfproperties");
b使用相对路径:
相对路径的根目录就是你的webapplication的根路径,即WEB-INF的上一级目录,将你的参数文件放
在yourwebapp\yourconfig\yourconfproperties,
这样使用:
new FileInputStream("/yourconfig/yourconfproperties");
这两种方式均可,自己选择。
(2)Tomcat
在类中输出SystemgetProperty("userdir");显示的是%Tomcat_Home%/bin
(3)Resin
不是你的JSP放的相对路径,是JSP引擎执行这个JSP编译成SERVLET
的路径为根比如用新建文件法测试File f = new File("ahtm");
这个ahtm在resin的安装目录下
(4)如何读相对路径哪?
在Java文件中getResource或getResourceAsStream均可
例:getClass()getResourceAsStream(filePath);//filePath可以是"/filename",这里的/代表web
发布根路径下WEB-INF/classes
默认使用该方法的路径是:WEB-INF/classes。已经在Tomcat中测试。
5读取文件时的相对路径,避免硬编码和绝对路径的使用。(来自网络)
51 采用Spring的DI机制获得文件,避免硬编码。
参考下面的连接内容:
>不要用userdir,这个是根据你的运行环境改变的。
我也做过很多有upload的项目,都是用property文件定义一个绝对路径去存放上传的文件的。服务器端没有必要使用相对路径。
客户端的东西都使用相对路径,因为对于客户端来说,绝对路径是服务器的绝对路径,客户端是不能访问的。
所以,不知道你为什么要在服务器端使用相对路径,对于web开发来说,是没有意义的。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)