JAVA 把文件传到服务器.......

JAVA 把文件传到服务器.......,第1张

文件上传到A以后 放到服务器上面 然后他就有一个绝对的访问路径 也就是对应一个绝对的url 这样就好办了

Java提供了对URL访问和大量的流操作的的API,可以很容易的完成对网络上资源的存取,下面的代码段就完成了对一个网站的资源进行访问:

destUrl="http://wwwyourwebcom/java/Afilezip";

//假设你把文件放到webroot底下的java文件里面

url = new URL(destUrl);

httpUrl = (HttpURLConnection) urlopenConnection();

//连接指定的网络资源

httpUrlconnect();

//获取网络输入流

bis = new BufferedInputStream(httpUrlgetInputStream());

得到流后下面你自己想怎么操作就怎么操作了

对于怎么得到资源的连接地址这个方法很多 你可以专门提供一个Servlet 获取到输出的流后 Responsewrite转门提供服务器已上传的文件 文件名可以一天位单位返回

客户端用与上面同样的方法得到文件名后 拆分 然后再继续循环调用上面的方法 下载文件就ok了

呵呵 希望可以帮助到你

如果服务器开通了ftp服务,你的客户端可以实现一个ftp的客户端,通过ftp服务将文件上传到服务器的指定目录下,可以使用orgapachecommonsnetftpFTPClient这个类去实现,非常的简单,网上有很多现成的代码可以用

package sockettest;

import javaio;

import javanet;

import javalangThread;

public class Client extends Thread{

private char[] buffer=new char[2048];

private Socket socket=null;

private String localIP="";

private String farIP="";

private int localPort=8765;

private int farPort=0;

private InputStream inKeyboard=Systemin;

private InputStreamReader readKeyboard=new InputStreamReader(inKeyboard);

private BufferedReader getKeyboard=new BufferedReader(readKeyboard); //得到键盘输入的数据

private InputStream in=null;

private FileInputStream inFile=null;

private InputStreamReader inRead=null;

private BufferedReader read=null;

private OutputStream out=null;

private FileOutputStream outFile=null;

private OutputStreamWriter outWrite=null;

private BufferedWriter write=null;

private int timeout=30000;

public void clearBuffer(){

for(int i=0;i<bufferlength;i++)

buffer[i]=0;

}

public Client()throws Exception {

socket=new Socket();

}

public Client(String ip,int port)throws Exception{

socket=new Socket(ip,port);

localIP=socketgetLocalAddress()getHostAddress();

localPort=socketgetLocalPort();

}

public void bind(String ip,int port)throws Exception{ //绑定工作

SocketAddress serverAddr=new InetSocketAddress(ip,port);

socketbind(serverAddr);

localIP=socketgetLocalAddress()getHostAddress();

localPort=socketgetLocalPort();

}

public void setTimeout(int time)throws Exception{

timeout=time;

socketsetSoTimeout(timeout);

}

public void begin()throws Exception{ //开始输入输出的变更封装

in=socketgetInputStream();

inRead=new InputStreamReader(in);

read=new BufferedReader(inRead);

out=socketgetOutputStream();

outWrite=new OutputStreamWriter(out);

write=new BufferedWriter(outWrite);

}

public void writeFromKeyboard()throws Exception{ //使用键盘的输入写到套接字连接 :限制每次128个字节

String s="";

s=getKeyboardreadLine();

writewrite(s);

writeflush();

}

public void writeFromFile(String filePath,long off,int len)throws Exception{ //将文件里的内容写到套接字连接,参数:文件,开始,长度

inFile=new FileInputStream(filePath);

InputStreamReader i=new InputStreamReader(inFile);

BufferedReader r=new BufferedReader(i);

rskip(off);

char[] data=new char[len];

int num=rread(data);

rclose();

writewrite(data,0,num);

writeflush();

}

public String readToPrint()throws Exception{ //读取套接字连接里的内容,显示到屏幕 :限制1024个字节

clearBuffer();

int num=readread(buffer);

String s=StringvalueOf(buffer,0,num);

return s;

}

public void readToFile(String filePath,boolean on)throws Exception{ //将读取的数据写到套接字连接中 参数:文件名, true(添加)

outFile=new FileOutputStream(filePath,on);

OutputStreamWriter o=new OutputStreamWriter(outFile);

BufferedWriter w=new BufferedWriter(o);

clearBuffer();

int num=readread(buffer);

String s=StringvalueOf(buffer,0,num);

wwrite(s);

wflush();

}

public void closeLink()throws Exception{ //关闭当前套接字连接

socketclose();

}

public String getLocalIP()throws Exception{ //返回本地地址

return localIP;

}

public int getLocalPort()throws Exception{ //返回本地端口

return localPort;

}

public String getFarIP()throws Exception{ //返回远程主机地址

farIP=socketgetInetAddress()getHostAddress();

return farIP;

}

public int getFarPort()throws Exception{ //返回远程主机端口

farPort=socketgetPort();

return farPort;

}

public void setBufferSize(int size){

buffer=new char[size];

}

}

---------------------------------------------------

package sockettest;

import javaio;

import javanet;

import javalangThread;

public class Server extends Thread{

private char[] buffer=new char[2048];

private ServerSocket server=null;

private Socket socket=null;

private String localIP="";

private String farIP="";

private int localPort=8765;

private int farPort=0;

private InputStream inKeyboard=Systemin;

private InputStreamReader readKeyboard=new InputStreamReader(inKeyboard);

private BufferedReader getKeyboard=new BufferedReader(readKeyboard); //得到键盘输入的数据

private InputStream in=null;

private FileInputStream inFile=null;

private InputStreamReader inRead=null;

private BufferedReader read=null;

private OutputStream out=null;

private FileOutputStream outFile=null;

private OutputStreamWriter outWrite=null;

private BufferedWriter write=null;

private int timeout=30000;

public void clearBuffer(){

for(int i=0;i<bufferlength;i++)

buffer[i]=0;

}

public Server(String ip,int port) throws Exception{ //参数:服务器地址 端口

SocketAddress serverAddr=new InetSocketAddress(ip,port);

server=new ServerSocket();

serverbind(serverAddr);

serversetSoTimeout(timeout);

}

public Server()throws Exception{

SocketAddress serverAddr=new InetSocketAddress("127001",8765);

server=new ServerSocket();

serverbind(serverAddr);

serversetSoTimeout(timeout);

}

public void bind(String ip,int port)throws Exception{ //绑定工作

SocketAddress serverAddr=new InetSocketAddress(ip,port);

serverbind(serverAddr);

}

public void accept()throws Exception{ //开始侦听端口

socket= serveraccept();

localIP=socketgetLocalAddress()getHostAddress();

localPort=socketgetLocalPort();

}

public void setTimeout(int time)throws Exception{

timeout=time;

serversetSoTimeout(timeout);

}

public void begin()throws Exception{ //开始输入输出的变更封装

in=socketgetInputStream();

inRead=new InputStreamReader(in);

read=new BufferedReader(inRead);

out=socketgetOutputStream();

outWrite=new OutputStreamWriter(out);

write=new BufferedWriter(outWrite);

}

public void writeFromKeyboard()throws Exception{ //使用键盘的输入写到套接字连接 :限制每次128个字节

String s="";

s=getKeyboardreadLine();

writewrite(s);

writeflush();

}

public void writeFromFile(String filePath,long off,int len)throws Exception{ //将文件里的内容写到套接字连接,参数:文件,开始,长度

inFile=new FileInputStream(filePath);

InputStreamReader i=new InputStreamReader(inFile);

BufferedReader r=new BufferedReader(i);

rskip(off);

char[] data=new char[len];

int num=rread(data);

rclose();

writewrite(data,0,num);

writeflush();

}

public String readToPrint()throws Exception{ //读取套接字连接里的内容,显示到屏幕 :限制1024个字节

clearBuffer();

int num=readread(buffer);

String s=StringvalueOf(buffer,0,num);

return s;

}

public void readToFile(String filePath,boolean on)throws Exception{ //将读取的数据写到套接字连接中 参数:文件名, true(添加)

outFile=new FileOutputStream(filePath,on);

OutputStreamWriter o=new OutputStreamWriter(outFile);

BufferedWriter w=new BufferedWriter(o);

clearBuffer();

int num=readread(buffer);

String s=StringvalueOf(buffer,0,num);

wwrite(s);

wflush();

}

public void closeLink()throws Exception{ //关闭当前套接字连接

socketclose();

}

public void closeServer()throws Exception{ //关闭套接字服务器

serverclose();

}

public String getLocalIP()throws Exception{ //返回本地地址

return localIP;

}

public int getLocalPort()throws Exception{ //返回本地端口

return localPort;

}

public String getFarIP()throws Exception{ //返回远程主机地址

farIP=socketgetInetAddress()getHostAddress();

return farIP;

}

public int getFarPort()throws Exception{ //返回远程主机端口

farPort=socketgetPort();

return farPort;

}

public void setBufferSize(int size){

buffer=new char[size];

}

}

1,问题引入

  在linux下开发的上传模块,在windows下无法运行,提示找不到文件目录或文件

2,原因

  主要是因为linux和windows下由于用到了绝对路径,而文件路径间隔符不一样导致的,,

3,解决方法

  使用Fileseparator做文件间隔符,

注意:

在java中\t \a \b 等一般都有特别含义,所以文件路径为:

String str = C:\atxt (1)

则如果直接创建文件

File file = new File(str); (2)

则在(1)中就会出错,

所以正确通用的的方法是:

String separator = Fileseparator;

String str1 = "C:"+separator+"atxt"; (或者String str1 = "C:\\atxt)

File file = new File(str1);

同理在linux下也是一样,,,

DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
网站模板库 » JAVA 把文件传到服务器.......

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情