这个怎么做啊,求指教
/*
* QQcustomer1.java
*
* Created on __DATE__, __TIME__
*/
package cn.itcast.customer;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Arrays;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
/**
*
* @author __USER__
*/
public class QQcustomer1 extends javax.swing.JFrame {
static Socket socket=null;
static String name="皮革";
static OutputStream os=null;
static ServerThread3 st3=null;
/** Creates new form QQcustomer1 */
public QQcustomer1() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton1 = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea2 = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jButton1.setText("\u4e0a\u4f20 ");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
try {
try {
jButton1ActionPerformed(evt);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
jTextArea2.setColumns(20);
jTextArea2.setRows(5);
jScrollPane2.setViewportView(jTextArea2);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout
.setHorizontalGroup(layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup().addContainerGap(
273, Short.MAX_VALUE).addComponent(
jButton1).addGap(66, 66, 66))
.addGroup(
layout
.createSequentialGroup()
.addGap(29, 29, 29)
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
false)
.addComponent(
jScrollPane2,
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(
jScrollPane1,
javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE,
334,
Short.MAX_VALUE))
.addContainerGap(37, Short.MAX_VALUE)));
layout
.setVerticalGroup(layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createSequentialGroup()
.addGap(24, 24, 24)
.addComponent(
jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED,
11, Short.MAX_VALUE)
.addComponent(
jScrollPane2,
javax.swing.GroupLayout.PREFERRED_SIZE,
106,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap()));
pack();
}// </editor-fold>
//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) throws IOException, InterruptedException {
// TODO add your handling code here:
// 弹出选择对话框
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setDialogTitle("请选择上传文件");
int result=chooser.showOpenDialog(null);
// 选择文件名与地址
String str_FilePath=null;
String str_FileName=null;
File file = chooser.getSelectedFile();
if(result==JFileChooser.APPROVE_OPTION){
str_FilePath =file.getAbsolutePath();
str_FileName=file.getName();
}
FileInputStream fis = new FileInputStream(str_FilePath);
byte[] buf = new byte[1024];
byte[] doc= new byte[0];
int len=0;
while( (len = fis.read(buf))!=-1){//1,787,350
//1 788 374
//如何将每次读到的内容放到一个大小 为1,787,350长度的字节数组
doc = Arrays.copyOf(doc, doc.length+len);
System.arraycopy(buf, 0, doc, doc.length-len, len);
}
// 把数据封装到customer
Customer cs=new Customer("罗成",str_FileName,doc);
//把customer对象发送过去
ObjectOutputStream ois = new ObjectOutputStream(os);
ois.writeObject(cs);
System.out.println("存在");
fis.close();
ois.flush();
os.close();
}
/**
* @param args the command line arguments
* @throws IOException
* @throws InterruptedException
*/
public static void main(String args[]) throws IOException, InterruptedException {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
QQcustomer1 inst = new QQcustomer1();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
String str_ip=InetAddress.getLocalHost().getHostAddress();
socket = new Socket(str_ip, 8888);
os=socket.getOutputStream();
os.write(name.getBytes());
os.flush();
st3=new ServerThread3(socket);
st3.start();
}
//GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextArea jTextArea2;
// End of variables declaration//GEN-END:variables
}
class ServerThread3 extends Thread{
private Socket s=null;
private Customer customer=null;
private File file=null;
private InputStream is;
public ServerThread3(Socket s) {
super();
this.s = s;
}
public void run(){
try{
while(true){
is=s.getInputStream();
ObjectInputStream osi = new ObjectInputStream(is);
// 接受customer对象
customer = (Customer) osi.readObject();
System.out.println(customer.getFileName());
// 判断customer对象是否存在
if(customer!=null){
int jssc=JOptionPane.showConfirmDialog(null,"是接受上传文件","是接受上传文件",JOptionPane.YES_NO_OPTION);
if(jssc==0){
// 弹出保存框
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setDialogTitle("请选择保存目录");
int result=chooser.showOpenDialog(null);
String str_FilePath=null;
if(result==JFileChooser.APPROVE_OPTION){
str_FilePath=chooser.getSelectedFile().getAbsolutePath();
}
String str_FileName=customer.getFileName();
file =new File(str_FilePath,str_FileName);
if(file.exists()){
int i=1;
int xuanzhe=JOptionPane.showConfirmDialog(null,"是否覆盖文件","是否覆盖文件",JOptionPane.YES_NO_OPTION);
// 不覆盖的话
if(!(xuanzhe==0)){
String i_str=null;
File fileallname=new File(str_FilePath);
//获取目录中的全部文件名来和传送过来的文件进行比较
String[] Documentname=fileallname.list();
String name1=str_FileName;
for(int k=0;k<Documentname.length;k++){
for(int j=0;j<Documentname.length;j++){
if(name1.equals(Documentname[j])){
i_str="("+i+")";
name1=i_str+str_FileName;
i++;
}
}
}
str_FileName=name1;
file =new File(str_FilePath,str_FileName);
file.exists();
}
// 覆盖的话
else{
file.exists();
}
}
}
}
else{
FileOutputStream fos = new FileOutputStream(file);
fos.write(customer.getBuf());//文件
fos.close();
}}
}
catch(Exception e){
}
}
} |
|