「java远程文件创建」java 远程文件
今天给各位分享java远程文件创建的知识,其中也会对java 远程文件进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、javaweb 怎么样将本地文件传输到远程服务器
- 2、java 怎么使用远程 url 创建 file
- 3、java如何在远程tomcat下的创建文件。。。。。。。急急急
- 4、java远程创建文件夹与文件
- 5、java 远程文件管理
javaweb 怎么样将本地文件传输到远程服务器
可以通过JDK自带的API实现,如下代码:
package com.cloudpower.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.;
/**
* Java自带的API对FTP的操作
* @Title:
*/
public class Ftp {
/**
* 本地文件名
*/
private String localfilename;
/**
* 远程文件名
*/
private String remotefilename;
/**
* FTP客户端
*/
private FtpClient ftpClient;
/**
* 服务器连接
* @param ip 服务器IP
* @param port 服务器端口
* @param user 用户名
* @param password 密码
* @param path 服务器路径
* @date 2012-7-11
*/
public void connectServer(String ip, int port, String user,
String password, String path) {
try {
/* ******连接服务器的两种方法*******/
//第一种方法
// ftpClient = new FtpClient();
// ftpClient.openServer(ip, port);
//第二种方法
ftpClient = new FtpClient(ip);
ftpClient.login(user, password);
// 设置成2进制传输
ftpClient.binary();
System.out.println("login success!");
if (path.length() != 0){
//把远程系统上的目录切换到参数path所指定的目录
ftpClient.cd(path);
}
ftpClient.binary();
} catch (IOException ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
public void closeConnect() {
try {
ftpClient.closeServer();
System.out.println("disconnect success");
} catch (IOException ex) {
System.out.println("not disconnect");
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
public void upload(String localFile, String remoteFile) {
this.localfilename = localFile;
this.remotefilename = remoteFile;
TelnetOutputStream os = null;
FileInputStream is = null;
try {
//将远程文件加入输出流中
os = ftpClient.put(this.remotefilename);
//获取本地文件的输入流
File file_in = new File(this.localfilename);
is = new FileInputStream(file_in);
//创建一个缓冲区
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
System.out.println("upload success");
} catch (IOException ex) {
System.out.println("not upload");
ex.printStackTrace();
throw new RuntimeException(ex);
} finally{
try {
if(is != null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(os != null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void download(String remoteFile, String localFile) {
TelnetInputStream is = null;
FileOutputStream os = null;
try {
//获取远程机器上的文件filename,借助TelnetInputStream把该文件传送到本地。
is = ftpClient.get(remoteFile);
File file_in = new File(localFile);
os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
System.out.println("download success");
} catch (IOException ex) {
System.out.println("not download");
ex.printStackTrace();
throw new RuntimeException(ex);
} finally{
try {
if(is != null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(os != null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String agrs[]) {
String filepath[] = { "/temp/aa.txt", "/temp/regist.log"};
String localfilepath[] = { "C:\\tmp\\1.txt","C:\\tmp\\2.log"};
Ftp fu = new Ftp();
/*
* 使用默认的端口号、用户名、密码以及根目录连接FTP服务器
*/
fu.connectServer("127.0.0.1", 22, "anonymous", "IEUser@", "/temp");
//下载
for (int i = 0; i filepath.length; i++) {
fu.download(filepath[i], localfilepath[i]);
}
String localfile = "E:\\号码.txt";
String remotefile = "/temp/哈哈.txt";
//上传
fu.upload(localfile, remotefile);
fu.closeConnect();
}
}
java 怎么使用远程 url 创建 file
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
/**
* @author lmq
*
*/
public class RemoteFile {
public static void main(String[] args) throws Exception {
File remoteFile = new File("//192.168.7.146/test/1.txt");// 192.168.7.146是对方机器IP,test是对方那个共享文件夹名字,如果没有共享是访问不到的
//远程文件其实主要是地址,地址弄对了就和本地文件没什么区别 ,windows里面//或者\\\\开头就表示这个文件是网络路径了其实这个地址就像我们再windows里面,点击开始
//然后点击运行,然后输入 \\192.168.7.146/test/1.txt访问远程文件一样的
BufferedReader br = new BufferedReader(new FileReader(remoteFile));
String str;
while ((str = br.readLine()) != null) {
System.out.println(str);
}
br.close();
}
}
如果是非共享文件 你只能通过url读取流来生成了
public void downUrlTxt(String fileName,String fileUrl,String downPath){
File savePath = new File(downPath);
if (!savePath.exists()) {
savePath.mkdir();
}
String[] urlname = fileUrl.split("/");
int len = urlname.length-1;
String uname = urlname[len];//获取文件名
try {
File file = new File(savePath+"/"+uname);//创建新文件
if(file!=null !file.exists()){
file.createNewFile();
}
OutputStream oputstream = new FileOutputStream(file);
URL url = new URL(fileUrl);
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setDoInput(true);//设置是否要从 URL 连接读取数据,默认为true
uc.connect();
InputStream iputstream = uc.getInputStream();
System.out.println("file size is:"+uc.getContentLength());//打印文件长度
byte[] buffer = new byte[4*1024];
int byteRead = -1;
while((byteRead=(iputstream.read(buffer)))!= -1){
oputstream.write(buffer, 0, byteRead);
}
oputstream.flush();
iputstream.close();
oputstream.close();
} catch (Exception e) {
System.out.println("读取失败!");
e.printStackTrace();
}
System.out.println("生成文件路径:"+downPath+fileName);
}
java如何在远程tomcat下的创建文件。。。。。。。急急急
不行的,除非你B的tomcat里跑着一个程序,这个程序可以让你上传文件,那么你通过a可以访问B来进行文件操作,要不然,你只能在a上通过ftp协议来访问B,然后可以进行文件操作,前提是你的b机器开启了ftp端口和服务
java远程创建文件夹与文件
两种思路:
1。直接用FILE类,但是有权限的问题。你要保证你登陆本地的帐户在远程电脑上也存在。
2。通过流传递过去,可以参考JAR包-FILEUPLOAD。
java 远程文件管理
可以哦 HTTP SERVLET就可以
不过 这样写出来的 文件读出来都是 字节流,可以在本地生成对应的文件...
我理解就是一个远程 文件读取工具...
要是想做 界面的话 就不知道怎么做了 我上面说的 程序上的..
java远程文件创建的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java 远程文件、java远程文件创建的信息别忘了在本站进行查找喔。