我要用java实现文件接收的功能,
现在别人给我的服务器传送文件,服务器接受之后保存到服务器的一个路径下面,现在没有request对象,如何实现这一流程????在线等
现在别人给我的服务器传送文件,服务器接受之后保存到服务器的一个路径下面,现在没有request对象,如何实现这一流程????在线等
解决方案 »
- 我想问个 J2EE里 幂等 的问题
- flex 嵌套 html 事件问题?
- 同步 怎么理解这两个的区别呀
- hibernate 问题
- jms的初级问题,希望有经验的朋友指点一二
- spring里面对hibernate的DAOSupport里的getSession(boolean)方法中布尔值代表什么?
- 乙肝带菌者软件公司不要吗?
- [STRUTS] 请问如何在STRUTS页面中创建自定义的标签?
- 前些日子写个struts的小项目,现在搬到公司的机器上就跑不起来了,不知道为什么...(error in opening zip file)
- EJB的初学问题,希望EJB的高手能给予指点,谢谢了!
- tomcat移植到resin上,出错!!!!『虚拟空间上跑struts2的项目能行吗??』
- 如何将Action中得到的值在页面上显示出来?
Socket行不行?
http://www.zhangpeng.com.cn/article.asp?id=39
基本原理如下网上很多
http://topic.csdn.net/t/20010301/13/77310.html我们将要通信的两端分成服务器和客户机端,即建立所谓的客户机/服务器编程模式。在服务器端必须先建立一个ServerSocket对象,然后等待客户机端的访问。而在客户机端,则是建立一个Socket对象直接跟服务器端连接,如果连接建立成功,则服务器端便会产生一个Socket对象,然后我们就可以利用这个Socket对象跟客户机端的Socket对象沟通了。此时在服务器和客户机之间建立了一条可靠连接,客户机和服务器可以在这条连接上可靠的传送数据。客户机发出请求,服务器监听来自客户机的请求,并为客户机提供相应的服务。
基于上述原理,我们编写了简单的客户机/服务器模式的网络通信程序。在服务器端,服务器监听客户机的请求,为每个客户机请求建立Socket连接,从而为客户机提供服务。而所提供的服务只是读取来自客户机的一行文本,并把它发回给客户机。以下是服务器端的通信程序。
import java.io.*;
import java.net.*;
class javaserver extends Thread {
ServerSocket server;
public javaserver() {
try {
server = new ServerSocket(600);
}
catch(IOException e) {
System.out.println("Cannot create Server");
System.exit(0);
}
System.out.println("Now socket server will Start");
this.start();
}
public void run() {
try {
while (true) {
Socket client = server.accept();
service ss = new service(client);
}
}
catch(IOException e) {
System.out.println("cannot provide service !");
System.exit(1);
}
}
public static void main(String args[]){
String data;
DataInputStream KeyInput;
new javaserver();
KeyInput = new DataInputStream(System.in);
try {
data = KeyInput.readLine();
}
catch (IOException e){
return;
}
if (data.equals("quit")) System.exit(1);
}
}
class service extends Thread {
String data;
DataInputStream InputS;
PrintStream OutputS;
Socket Client;
public service(Socket ClientSocket) {
Client = ClientSocket;
try {
InputS = new DataInputStream
(Client.getInputStream());
OutputS = new PrintStream
(Client.getOutputStream());
}
catch (IOException e){
System.out.println("Cannot Connect with Client !");
return;
}
this.start();
}
public void run(){
try {
while (true){
data = InputS.readLine();
if (data == null) break;
else {
OutputS.println(data);
System.out.println("From Client: " + data);
}
}
}
catch (IOException e){
System.out.println("Read Data error");
}
try {
Client.close();
}
catch (IOException e){
System.out.println("Cannot close socket");
}
}
}
在上面的程序中,我们使用了多线程机制。javaserver和service对象本身都是一个线程。javaserver对象首先创建一个ServerSocket对象,并启动线程的运行。它的run()方法用于监听来自客户机的连接。每当有一个新的客户机连接时,ServerSocket就会创建一个新的Socket类实例,并创建一个service对象,同时启动这个对象的线程。每个service对象用于完成与客户机通信、提供服务的任务。这样服务器可以同时与多个客户机连接,同时为多个客户机提供服务。当从标准输入中接收到quit字符串时,服务器退出运行。
在客户机端,首先创建一个Socket对象,用于与服务器通信。它从标准输入中读取数据,把这些数据传给服务器,再从服务器读取应答信息,然后把这些应答信息写到标准输出。当读取了5行的数据后,客户机程序将退出运行。以下是客户机端的通信程序。
import java.io.*;
import java.net.*;
class javaclient {
public static void main(String args[]){
String data;
Socket Client;
DataInputStream InputS;
DataInputStream KeyS;
PrintStream OutputS;
int i = 0;
try {
Client = new Socket("172.17.3.2",600);
InputS = new
DataInputStream(Client.getInputStream());
OutputS = new PrintStream
(Client.getOutputStream());
KeyS = new DataInputStream(System.in);
}
catch(IOException e){
System.out.println("Cannot Connect
with Server");
return;
}
try {
while (i<5){
data = KeyS.readLine();
OutputS.println(data);
System.out.println("ECHO From
Server:"+ InputS.readLine());
i++;
}
}
catch(IOException e) {
System.out.println("IOException
Happened");
}
try{
System.out.println("Now will
end this program");
Client.close();
}
catch(IOException e){
System.out.println("system cannot
close socket");
}
}
}
new一个ServerSocket(Localhost,21)
他监听21端口,要是接收到ftp文件就会得到他的流
socket.getInputStream());
然后读到缓存然后写到你要保存的文件里去,这个思路可以吗?我也没真正写过,只有个思路
我现在是这种情况,跟QQ一样,用户传送一个文件过来,然后程序会执行一个方法public void fileReceiveStarted(VolatileDownloader downloader) {
File f = downloader.getFile();
String name = f.getName();
String filePath = "C:\\filepath";
File myFile = new File(filePath);
if(!myFile.exists()) {
myFile.mkdir();
}
System.out.println("path = " + downloader.getPort()+"hostname = " + downloader.getHostAddress());
System.out.println("name = " + downloader.getFile().getAbsolutePath());
try {
// InputStream instream = new FileInputStream(f);
// byte[] bytes = new byte[(int)f.length()];
// instream.read(bytes);
// OutputStream outstream = new FileOutputStream(filePath+"\\" + name);
// BufferedOutputStream bs = new BufferedOutputStream(outstream);
// bs.write(bytes);
// instream.close();
// outstream.close();
// bs.close();
Socket socket = new Socket(downloader.getHostAddress(),downloader.getPort());
InputStream instream = socket.getInputStream();
byte[] bytes = new byte[(int)f.length()];
instream.read(bytes);
OutputStream outstream = new FileOutputStream(filePath+"\\" + name);
int size = (int)f.length();
int count = 0;
while (count < size) {
int n = instream.read(bytes);System.out.println(n);
// 这里没有考虑 n = -1 的情况
if(n > -1) {
outstream.write(bytes, 0, n);
count += n;
}
}
instream.close();
outstream.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println("文件接收方法:fileReceiveStarted" + ":path = ");
}
也就是执行上面的那个方法,即在这个方法里面实现将文件保存到本地的服务器上,这个有人会吗?