当前位置:首页 » 文件管理 » java远程上传文件

java远程上传文件

发布时间: 2022-09-05 08:13:13

java中用Socket远程传输文件出现连接超时的异常

链接到远程的服务器端是可以的,ip地址填服务器端的ip,端口号,服务器端要设置的和你的客户端一样。

❷ Java文件上传报错:远程主机强迫关闭了一个现有的连接

是不是前端哪儿写错了,如果是走不到断点的话,那就是MutilpartFile 没有接受到文件,是不是前端上传文件的插件中有个文件名要和MultipartFile 的file名字一样啊!

❸ 怎么利用java将本地文件上传至远程服务器,求源码,要实验过的,谢谢

将本地文件上传到远程服务器windows一般用ftplinux用ssh。
-黑客榜上榜

❹ java上传图片到远程服务器上,怎么解决呢

不好实现,网上有方法说用FTP,但是不会用啊,找了一个 public static void forcdt(String dir){ InputStream in = null; OutputStream out = null; File localFile = new File(dir);try{//创建file类 传入本地文件路径 //获得本地文件的名字 String fileName = localFile.getName(); //将本地文件的名字和远程目录的名字拼接在一起 //确保上传后的文件于本地文件名字相同 SmbFile remoteFile = new SmbFile("smb://administrator:[email protected]/e$/aa/"); //创建读取缓冲流把本地的文件与程序连接在一起 in = new BufferedInputStream(new FileInputStream(localFile)); //创建一个写出缓冲流(注意jcifs-1.3.15.jar包 类名为Smb开头的类为控制远程共享计算机"io"包) //将远程的文件路径传入SmbFileOutputStream中 并用 缓冲流套接 out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile+"/"+fileName)); //创建中转字节数组 byte[] buffer = new byte[1024]; while(in.read(buffer)!=-1){//in对象的read方法返回-1为 文件以读取完毕 out.write(buffer); buffer = new byte[1024];}}catch(Exception e){ e.printStackTrace();}finally{try{//注意用完操作io对象的方法后关闭这些资源,走则 造成文件上传失败等问题。! out.close(); in.close();

❺ java怎么实现上传附件的功能

上传附件,实际上就是将文件存储到远程服务器,进行临时存储。举例:
**
* 上传文件
*
* @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);
}
}
}
}
备注:只需要修改上传的服务器地址、用户名、密码即可进行服务器访问上传。根据实际需要修改即可。

❻ java向SFTP服务器上传文件,如何判断服务器上的文件夹是否存在

  • 如果你的JAVA部署的tomcat,就是你要查找文件的服务器,那就用:

    File file = new File("文件路径")。

  • 如果你本地的JAVA想要访问远程的一个服务器的文件是否存在,就得用如下方法:

    URL url = new URL(“文件路径:可以是本地服务器的路径,也可以是远程服务器的路径”)

    HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();

    //message = urlcon.getHeaderField(0);

    //文件存在‘HTTP/1.1 200 OK’ 文件不存在 ‘HTTP/1.1 404 Not Found’

    Long TotalSize=Long.parseLong(urlcon.getHeaderField("Content-Length"));

    if (TotalSize>0){

    return true;

    }else{

    return false;

    }

❼ Java怎么上传文件到远程Windows服务器

安装一个FTP就可以直接上传下载了,你可以去服务器厂商(正睿)的网上找找相关技术文档参考一下,很快就清楚了!

❽ JAVA_JSCH如何远程操作SFTP服务器上的文件

使用SSH协议进行FTP传输的协议叫SFTP
换言之你的SSH协议一定启用了,那么使用基本linux命令在远端执行即可。
我个人而言,JSCH一般是这样用的:SFTP用于单纯的文件上传,之后直接使用基础ssh协议执行远端linux命令(比如说,移动文件或是重启服务器等等)
至于API的具体使用方式,稍微搜索一下很容易找到,比如这个:
http://blog.csdn.net/allen_zhao_2012/article/details/7941631

❾ 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", "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();
}
}

热点内容
网站编程培训 发布:2024-10-12 06:09:22 浏览:900
怎么看自己的电脑配置玩永劫无间 发布:2024-10-12 05:56:41 浏览:467
linuxzip文件解压命令 发布:2024-10-12 05:56:03 浏览:942
java怎么处理高并发 发布:2024-10-12 05:55:25 浏览:765
五子棋java源码 发布:2024-10-12 05:37:13 浏览:175
pythonopenstack怎么配置 发布:2024-10-12 05:16:07 浏览:929
安卓如何编辑动画 发布:2024-10-12 05:14:25 浏览:348
视频电脑配置高怎么玩游戏 发布:2024-10-12 04:35:56 浏览:731
sql复合查询 发布:2024-10-12 04:14:23 浏览:715
把文档加密 发布:2024-10-12 04:13:52 浏览:850