A. java文件上传报错:远程主机强迫关闭了一个现有的连接
是不是前端哪儿写错了,如果是走不到断点的话,那就是MutilpartFile 没有接受到文件,是不是前端上传文件的插件中有个文件名要和MultipartFile 的file名字一样啊!
B. java远程读写文件详解
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();
}
}
希望能帮到你。
C. 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();
}
}
D. java上传图片到远程服务器上,怎么解决呢
需要这样的一个包 jcifs-1.1.11
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();
}catch(Exception e){
e.printStackTrace();}
}
}
E. java项目中文件的上传与读取
互联网项目一般会有单独的服务器存放静态资源,图片就是一种静态资源,在这里就是区别于项目部署的另一台服务器。这时候你项目里面都是使用相对路径,像你上面所说的常量/opt/upload/这种做法是正确的,上传图片的时候,常见的有使用日期分目录存储的,如/opt/upload/2014/11/03/***.jpg,对于图片的路径,数据库里一般来说保存到2014/11/03/***.jpg就可以了。这是存图片。
取图片,一般来说资源都必须发布服务才能让外网访问。例如,你可以在你项目中写个servlet用来读取图片,如下面的服务地址http://ip:port/projectname/image/2014/11/03/***.jpg,其中2014前面的路径是固定的,后面的是你数据库里存储的图片地址,这时你页面代码里面只需固定前缀http://ip:port/projectname/image + 图片相对地址则可将图片读出来。
上面这种方法用的其实比较少,一般来说静态服务器都会部署一个web容器,然后使用单独的域名,打个比方,你在静态服务器上有个tomcat,目录/opt/tomcat/webapp/staticprojectname,staticprojectname是工程名,然后在上传的所有图片在staticprojectname下面,例如/opt/tomcat/webapp/staticprojectname/2014/11/03/***.jpg,然后在你原工程里面直接使用http://静态服务ip:port/staticprojectname/2014/11/03/***.jpg就可以访问到图片了,同样的在你代码里面2014前面的地址是固定的,配置成常量,后面的则是数据库里存的图片相对地址。
说了这么多,有点乱,希望你能明白
F. 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();
G. Java怎么上传文件到远程Windows服务器
安装一个FTP就可以直接上传下载了,你可以去服务器厂商(正睿)的网上找找相关技术文档参考一下,很快就清楚了!