100分求mq发送和接收文件的java代码或者实例 我把文件转换成类发出去 那边也能收到 也有字节数 可是写个程序把消息接收过来转换成文件就成了0字节到底为什么啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是我毕业设计中的关于文件传输用的代码,你可以参考参考import java.io.*; import java.net.*;import java.util.zip.*; public class FileServer extends Thread{ private String fileName; private int port; private File file; private FileInputStream fis; private ServerSocket ss; private Socket client; private OutputStream os; private ZipOutputStream zos; private DataOutputStream dos; private byte[] buf; public FileServer(String _fileName, int _port){ fileName=_fileName; port=_port; } public void run(){ try{ file=new File(fileName); fis=new FileInputStream(file); } catch(FileNotFoundException e1){ System.out.println("错误1:文件未找到!"); } try{ ss=new ServerSocket(port); client=ss.accept(); os=client.getOutputStream(); zos=new ZipOutputStream(os); zos.setMethod(ZipOutputStream.DEFLATED); zos.putNextEntry(new ZipEntry("zip")); dos=new DataOutputStream(zos); buf=new byte[2048]; int num=fis.read(buf); while(num!=(-1)){ dos.write(buf,0,num); dos.flush(); num=fis.read(buf); } fis.close(); dos.close(); } catch(IOException e2){ System.out.println("错误2:IOException!"+e2); } } /*测试用代码 public static void main(String[] args)throws Exception{ FileServer fs=new FileServer("F:\\graduation project\\思路.txt",3108); fs.start(); }*/ } import java.io.*;import java.net.*;import java.util.zip.*; public class FileClient extends Thread{ private String fileName; private String location; private String serverAddress; private File file; private RandomAccessFile raf; private Socket server; private InputStream is; private ZipInputStream zis; private DataInputStream tmpDis,dis; private byte[] buf; private int port; public FileClient(String _serverAddress,int _port,String _location,String _fileName){ serverAddress=_serverAddress; port=_port; location=_location; fileName=_fileName; } public void run(){ try{ file=new File(location+"\\"+fileName); file.createNewFile(); raf=new RandomAccessFile(file,"rw"); } catch(FileNotFoundException e1){ System.out.println("错误1:文件未找到!"); } catch(IOException e2){ System.out.println("错误2:IOException!"); } try{ server=new Socket(serverAddress,port); is=server.getInputStream(); tmpDis=new DataInputStream(is); zis=new ZipInputStream(tmpDis); zis.getNextEntry(); dis=new DataInputStream(zis); buf=new byte[2048]; int num=dis.read(buf); while(num!=(-1)){ raf.write(buf,0,num); raf.skipBytes(num); num=dis.read(buf); } dis.close(); raf.close(); } catch(IOException e3){ System.out.println("错误3:IOException!"+e3); } }/* 测试用代码 public static void main(String[] args)throws Exception{ FileClient fc=new FileClient(InetAddress.getLocalHost().getHostAddress(),"F:\\graduation project\\filetrans","newfile.txt"); fc.start(); }*/ }可以传输文件,由于还在测试,可能还有问题未找出贴出来,大家共同讨论 楼上的你这是socket 通信,不是mq的消息通信吧! 求助 JAVA UDP 通信的一个小问题 new Stirng效率问题 目前学习java还是.net好(java与.net对比) 连接池加载驱动 求大虾帮忙 java中怎么调用扫描仪 如何在JInternalFrame中创建一个模态的JDialog??? j2se;j2ee;j2me 是什么? 请教:node.cloneNode(boolean deep)如何用 帮忙!! jdk 1.4.2中如何获取系统环境变量? 客户端socket
import java.io.*;
import java.net.*;
import java.util.zip.*;
public class FileServer extends Thread{
private String fileName;
private int port;
private File file;
private FileInputStream fis;
private ServerSocket ss;
private Socket client;
private OutputStream os;
private ZipOutputStream zos;
private DataOutputStream dos;
private byte[] buf;
public FileServer(String _fileName, int _port){
fileName=_fileName;
port=_port;
}
public void run(){
try{
file=new File(fileName);
fis=new FileInputStream(file);
}
catch(FileNotFoundException e1){
System.out.println("错误1:文件未找到!");
}
try{
ss=new ServerSocket(port);
client=ss.accept();
os=client.getOutputStream();
zos=new ZipOutputStream(os);
zos.setMethod(ZipOutputStream.DEFLATED);
zos.putNextEntry(new ZipEntry("zip"));
dos=new DataOutputStream(zos);
buf=new byte[2048];
int num=fis.read(buf);
while(num!=(-1)){
dos.write(buf,0,num);
dos.flush();
num=fis.read(buf);
}
fis.close();
dos.close();
}
catch(IOException e2){
System.out.println("错误2:IOException!"+e2);
}
}
/*测试用代码
public static void main(String[] args)throws Exception{
FileServer fs=new FileServer("F:\\graduation project\\思路.txt",3108);
fs.start();
}
*/
}
import java.net.*;
import java.util.zip.*;
public class FileClient extends Thread{
private String fileName;
private String location;
private String serverAddress;
private File file;
private RandomAccessFile raf;
private Socket server;
private InputStream is;
private ZipInputStream zis;
private DataInputStream tmpDis,dis;
private byte[] buf;
private int port;
public FileClient(String _serverAddress,int _port,String _location,String _fileName){
serverAddress=_serverAddress;
port=_port;
location=_location;
fileName=_fileName;
}
public void run(){
try{
file=new File(location+"\\"+fileName);
file.createNewFile();
raf=new RandomAccessFile(file,"rw");
}
catch(FileNotFoundException e1){
System.out.println("错误1:文件未找到!");
}
catch(IOException e2){
System.out.println("错误2:IOException!");
}
try{
server=new Socket(serverAddress,port);
is=server.getInputStream();
tmpDis=new DataInputStream(is);
zis=new ZipInputStream(tmpDis);
zis.getNextEntry();
dis=new DataInputStream(zis);
buf=new byte[2048];
int num=dis.read(buf);
while(num!=(-1)){
raf.write(buf,0,num);
raf.skipBytes(num);
num=dis.read(buf);
}
dis.close();
raf.close();
}
catch(IOException e3){
System.out.println("错误3:IOException!"+e3);
}
}
/* 测试用代码
public static void main(String[] args)throws Exception{
FileClient fc=new FileClient(InetAddress.getLocalHost().getHostAddress(),"F:\\graduation project\\filetrans","newfile.txt");
fc.start();
}
*/
}可以传输文件,由于还在测试,可能还有问题未找出
贴出来,大家共同讨论