给你个例子看看。
发送端代码:
import java.net.*;
import java.io.*;/**
 * UDPSender is an implementation of the Sender interface, using UDP as the transport protocol.
 * The object is bound to a specified receiver host and port when created, and is able to 
 * send the contents of a file to this receiver.
 *
 * @author Alex Andersen ([email protected])
 */
public class TCPSender implements Sender{
    private File theFile;
    private FileInputStream fileReader;
    private Socket s;
    private int fileLength, currentPos, bytesRead, toPort, length;
    private byte[]  msg, buffer;
    private String toHost,initReply;
    private InetAddress toAddress;
    private OutputStream theOutstream; 
    private InputStream theInstream;    /**
     * Class constructor.
     * Creates a new UDPSender object capable of sending a file to the specified address and port.
     *
     * @param address  the address of the receiving host
     * @param port    the listening port on the receiving host
     */
    public TCPSender(InetAddress address, int port) throws IOException{
toPort = port;
toAddress = address;
msg = new byte[8192];
buffer = new byte[8192];
s = new Socket(toAddress, toPort);
theOutstream = s.getOutputStream();
theInstream = s.getInputStream();
    }
        /**
     * Sends a file to the bound host.
     * Reads the contents of the specified file, and sends it via UDP to the host 
     * and port specified at when the object was created.
     *
     * @param theFile  the file to send
     */
    public void sendFile(File theFile) throws IOException{
// Init stuff
fileReader = new FileInputStream(theFile);
fileLength = fileReader.available();

System.out.println(" -- Filename: "+theFile.getName());
System.out.println(" -- Bytes to send: "+fileLength); // 1. Send the filename and length to the receiver
theOutstream.write((theFile.getName()+"::"+fileLength).getBytes());
theOutstream.flush();
// 2. Wait for a reply from the receiver
System.out.println(" -- Waiting for OK from the receiver");
length = 0;
while (length <= 0){
    length = theInstream.read(buffer);
    if (length>0) initReply = (new String(buffer, 0, length));
}


// 3. Send the content of the file
if (initReply.equals("OK"))
    {
System.out.println("  -- Got OK from receiver - sending the file ");
while (currentPos<fileLength){
    //System.out.println("Will read at pos: "+currentPos);
    bytesRead = fileReader.read(msg);
    theOutstream.write(msg);
    //System.out.println("Bytes read: "+bytesRead);
    currentPos = currentPos + bytesRead;
}


System.out.println("  -- File transfer complete...");
    }
else{System.out.println("  -- Recieved something other than OK... exiting");}
    }
}接收端代码:
import java.net.*;
import java.io.*;
import java.util.*;public class TCPReceiver{
    int length;
    ServerSocket listener;
    Socket s;
    String filename, initString;
    byte[] buffer;
    FileOutputStream fileWriter;
    int bytesReceived, bytesToReceive;
    InputStream theInstream;
    OutputStream theOutstream;    public TCPReceiver(int port) throws IOException
    {
// Init stuff
listener = new ServerSocket(port);
buffer = new byte[8192];

System.out.println(" -- Ready to receive file on port: "+port);

s = listener.accept();
theInstream = s.getInputStream();
theOutstream = s.getOutputStream(); // 1. Wait for a sender to transmit the filename length = theInstream.read(buffer);
initString = "Recieved-"+new String(buffer, 0, length);
StringTokenizer t = new StringTokenizer(initString, "::");
filename = t.nextToken();
bytesToReceive = new Integer(t.nextToken()).intValue();

System.out.println("  -- The file will be saved as: "+filename);
System.out.println("  -- Expecting to receive: "+bytesToReceive+" bytes");


// 2. Send an reply containing OK to the sender
theOutstream.write((new String("OK")).getBytes());
System.out.println("send something");


// 3. Receive the contents of the file
 fileWriter = new FileOutputStream(filename);

while(bytesReceived < bytesToReceive)
    {
length = theInstream.read(buffer);
fileWriter.write(buffer, 0,  length);
bytesReceived = bytesReceived + length;
    }
System.out.println("  -- File transfer complete.");
    }   
}

解决方案 »

  1.   

    String msg = "PHONE=15270018560&AGENT=&BEGD=20140301&ENDD=201340301;";
    String[] str = {"FFFF","36","09","5A","00","00","31","30"};

    char[] result = new char[8];
    for (int i = 0; i < str.length; i++) {
    result[i] = (char)Integer.parseInt(str[i], 16);
    System.out.println(result[i]);
    }
    System.out.println(msg);

    PrintWriter out = new PrintWriter( new FileWriter( "D:\\1.txt" ) );
    out.write(result[0]);
    out.write(result[1]);
    out.write(result[2]);
    out.write(result[3]);
    out.write(result[4]);
    out.write(result[5]);
    out.write("1000");
    out.write("156       ");
    out.write("                    ");
    out.write("                    ");
    out.write(result[6]);
    out.write(";    ");
    out.write("&    ");
    out.write("        ");
    out.write("        ");
    out.write(msg);
    out.write(result[0]);
    out.close();各位大神帮看一下呗,我输入了几个十六进制字符串,然后转成ASCII码再插入到文本里,可以转的,但是我用JAVA不同的类,转完后FFFF这个竟然是不一样的,PrintWriter类和File类的文本输出就不一样的,我需要的是File这个类生成文件的内容,但是生成后每个字符前多了一个空格,求指教