java远程复制文件|通过java如何操作远程的linux服务器中的文件

|

❶ 我要用java实现远程文件复制

那还是得用Socket,URI,只是资源定位符,标志资源的位置,没法完成相关的操作。

❷ 如何使用java将远程数据库中的数据复制到本地数据库。

建立远程连接,要保证远程的连接是访问开放的,如果是mysql一般需要按照IP地址执行一次授权访问。然后按照数据格式进行一次本地的数据存储,面向对象转化为面向关系。

❸ java远程读写文件详解

实现上传下载实际上就是io的转换。举例:import hkrt.b2b.view.util.Log;import hkrt.b2b.view.util.ViewUtil;import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPFile;public class CCFCCBFTP {/*** 上传文件** @param fileName* @param plainFilePath 明文文件路径路径* @param filepath* @return * @throws Exception*/public static String fileUploadByFtp(String plainFilePath, String fileName, String filepath) throws Exception {FileInputStream fis = null;ByteArrayOutputStream bos = null;FTPClient ftpClient = new FTPClient();String bl = "false";try {fis = new FileInputStream(plainFilePath);bos = new ByteArrayOutputStream(fis.available());byte[] buffer = new byte[1024];int count = 0;while ((count = fis.read(buffer)) != -1) {bos.write(buffer, 0, count);}bos.flush();Log.info("加密上传文件开始");Log.info("连接远程上传服务器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22);ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22);ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSword);FTPFile[] fs;fs = ftpClient.listFiles();for (FTPFile ff : fs) {if (ff.getName().equals(filepath)) {bl="true";ftpClient.changeWorkingDirectory("/"+filepath+"");}}Log.info("检查文件路径是否存在:/"+filepath);if("false".equals(bl)){ViewUtil.dataSEErrorPerformedCommon( "查询文件路径不存在:"+"/"+filepath);return bl;}ftpClient.setBufferSize(1024);ftpClient.setControlEncoding("GBK");// 设置文件类型(二进制)ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);ftpClient.storeFile(fileName, fis);Log.info("上传文件成功:"+fileName+"。文件保存路径:"+"/"+filepath+"/");return bl;} catch (Exception e) {throw e;} finally {if (fis != null) {try {fis.close();} catch (Exception e) {Log.info(e.getLocalizedMessage(), e);}}if (bos != null) {try {bos.close();} catch (Exception e) {Log.info(e.getLocalizedMessage(), e);}}}}/***下载文件** @param localFilePath* @param fileName* @param routeFilepath* @return * @throws Exception*/public static String fileDownloadByFtp(String localFilePath, String fileName,String routeFilepath) throws Exception {FileInputStream fis = null;ByteArrayOutputStream bos = null;FileOutputStream fos = null;FTPClient ftpClient = new FTPClient();String SFP = System.getProperty("file.separator");String bl = "false";try {Log.info("下载并解密文件开始");Log.info("连接远程下载服务器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22);ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22);ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD);// ftpClient.connect(CMBCUtil.CMBCHOSTNAME, 2021);// ftpClient.login(CMBCUtil.CMBCLOGINNAME, CMBCUtil.CMBCLOGINPASSWORD);FTPFile[] fs; ftpClient.makeDirectory(routeFilepath);ftpClient.changeWorkingDirectory(routeFilepath);bl = "false";fs = ftpClient.listFiles();for (FTPFile ff : fs) {if (ff.getName().equals(fileName)) {bl = "true";Log.info("下载文件开始。");ftpClient.setBufferSize(1024);// 设置文件类型(二进制)ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);InputStream is = ftpClient.retrieveFileStream(fileName);bos = new ByteArrayOutputStream(is.available());byte[] buffer = new byte[1024];int count = 0;while ((count = is.read(buffer)) != -1) {bos.write(buffer, 0, count);}bos.flush();fos = new FileOutputStream(localFilePath+SFP+fileName);fos.write(bos.toByteArray());Log.info("下载文件结束:"+localFilePath);}}Log.info("检查文件是否存:"+fileName+" "+bl);if("false".equals(bl)){ViewUtil.dataSEErrorPerformedCommon("查询无结果,请稍后再查询。");return bl;}return bl;} catch (Exception e) {throw e;} finally {if (fis != null) {try {fis.close();} catch (Exception e) {Log.info(e.getLocalizedMessage(), e);}}if (bos != null) {try {bos.close();} catch (Exception e) {Log.info(e.getLocalizedMessage(), e);}}if (fos != null) {try {fos.close();} catch (Exception e) {Log.info(e.getLocalizedMessage(), e);}}}}}备注:以上方法就实现了流的二进制上传下载转换,只需要将服务器连接部分调整为本地的实际ftp服务用户名和密码即可。

❹ java如何导入数据库DMP文件到远程服务器上

方法1,imp是oracle提供的系统命令,在cmd下可以调用,故可以通过java.lang.Runtime包里的exec来实现。回具体你去看答看手册。这个方法的前提条件是必须服务器在cmd下能执行imp命令,如果是linux的服务器,也只要能执行同样调用。方法2,通过sql语句实现。就是自己实现备份,恢复。不使用oracle内部的命令。原理就是检索出所有的对象,然后写入文件,这里要注意分批导出与分批导入(也就是多个文件),不然数据量大了,速速就很慢。导出数据,存储过程,触发器,视图,权限等。这个就看你的需求了。。然后在分析文件实现导入。。3,自己编写系统程序,实现导入功能,仍然通过Runtime.exec来调用。4,因为imp是系统命令,你用stmt来调用是不对的,这个stmt只能执行sql语句,是oracle来分析的,所以会报错不是有效的sql语句。你换runtime.exec来执行,我想应该会成功的。。。

❺ Java怎么通过远程读取流的方式将远程文件放到本地 急急急!! 谢谢啦

你说的远程是Web服务器么?如果是Web服务器就用URLConnection,前提是知道远程文件的URL地址内;如果是CS架构容的服务就用Socket编程,前提是知道IP地址和服务端口以及请求的通讯报文格式。其他还有FTP服务等,情况不一样的。你需要把问题具体化。

❻ 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.ftp.FtpClient;/** * Java自带的API对FTP的操作 * @Title:Ftp.java*/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", "[email protected]", "/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类远程拷贝文件

直接复制 那是肯定不行 有安全限制建议是设置共享 复制过来java的Runtime.getRuntime().exec(commandText)可以调用执行cmd指令。 try { String cmdStr = "cmd /c \\192.168.7.77\111$\sss.doc c:\\" ; Runtime.getRuntime().exec(cmdStr); }catch(Exception e){ e.printStackTrace(); }如果是需要登版录 那就先用权 cmd指令 net use 。。。。就可以满足你要求了

❽ 通过java如何操作远程的linux服务器中的文件

在Linux服务器上开个FTP吧,然后Java通过FTP去访问。这样比较简单。通过ssh也可以,找个Java的ssh Jar包

❾ 如何使用java远程传输文件,client只提供ip\文件路径等参数,server端无需部署服务!

其实有几种方式的,1 ftp传输应用情况,加入在linux系统端有一些文件需要下载到用户电脑专client端,而linux系统又不是web服务器,属那么可以通过java程序FTP方式登录到linux,读取文件转换为流输出到用户IE端, java访问Linux服务器读取文件 所需jar包:j2ssh-core-0.2.2.jar2 socket方式,可以应用于比如server-client 聊天窗,传输文字;3 http协议,这种就是最常用的了,比如打开IE下载,上传东西,java是通过jsp servlet来实现的,然后部署放在tomcat web 服务器上,在其他局域网环境下的电脑登录IE即可访问到。没有特殊jar,只用java servlet的jar即可。例子如附件,可能上传不成功哈,网络网络会有很多哈

❿ Java怎么通过远程读取流的方式将远程文件放到本地

以下回答为本人意见,如果有误还请见谅。java获取远程文件的方式在我的开发过程中使版用过两种1。通权过http请求进行静态资源,首先确定文件的URL地址,然后通过URLConnection进行连接,然后通过读取连接中返回的InputStream,再通过文件输出流FileOutputStream进行存储(下载)。2.通过FTP或SFTP进行远程文件的下载,具体实现有很多第三方的包,网络即可。


赞 (0)