java.lang.ClassCastException: java.net.SocketInputStream cannot be cast to java.lang.CharSequence
importjavax.swing.*;importjava.io.Serializable;importjava.util.concurrent.ArrayBlocki...
import javax.swing.*;
import java.io.Serializable;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import com.mysql.jdbc.PreparedStatement;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.lang.*;
public class serve3 extends JFrame implements ActionListener {
Connection con = null;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/aaa";
String user = "root";
String password = "123456";
JTextArea jta=null;
JTextField jtf=null;
JButton jb=null;
JPanel jp=null;
JScrollPane jsp=null;
PrintWriter pw=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
serve3 s=new serve3();
}
public serve3(){
jta=new JTextArea();
jsp=new JScrollPane(jta);
jtf=new JTextField(20);
jb=new JButton("发送");
jb.addActionListener(this);
jp=new JPanel();
jp.add(jtf);
jp.add(jb);
this.add(jsp,"Center");
this.add(jp,"South");
this.setSize(400,300);
this.setTitle("QQ简易版 服务器");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
Class.forName(driver);
System.out.println("H");
Connection conn = DriverManager.getConnection(url, user,
password);
Statement stat = conn.createStatement();
PreparedStatement ps1 = (PreparedStatement) conn
.prepareStatement("insert mytable (file) values(?)");
//File file1 = new File("E:\\wps office\\ppt\\Decline.doc");
//InputStream inn = new FileInputStream(file1);
ServerSocket ss=new ServerSocket(9899);
Socket s=ss.accept();
System.out.println("连接成功");
InputStream inn=s.getInputStream();
//InputStreamReader isr=new InputStreamReader(s.getInputStream());
//BufferedReader br=new BufferedReader(isr);
//pw=new PrintWriter(s.getOutputStream(),true);
//
//while(true){
//
//String info=br.readLine();
//jta.append("客户端 对 服务器 说:"+info+"\r\n");
//
//}
ps1.setBinaryStream(1, inn,((CharSequence) inn).length());
ps1.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jb){
String info=jtf.getText();
jta.append("服务器 对 客户端 说:"+info+"\r\n");
pw.println(info);
jtf.setText("");
}
}
}
求大神指教啊 展开
import java.io.Serializable;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import com.mysql.jdbc.PreparedStatement;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.lang.*;
public class serve3 extends JFrame implements ActionListener {
Connection con = null;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/aaa";
String user = "root";
String password = "123456";
JTextArea jta=null;
JTextField jtf=null;
JButton jb=null;
JPanel jp=null;
JScrollPane jsp=null;
PrintWriter pw=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
serve3 s=new serve3();
}
public serve3(){
jta=new JTextArea();
jsp=new JScrollPane(jta);
jtf=new JTextField(20);
jb=new JButton("发送");
jb.addActionListener(this);
jp=new JPanel();
jp.add(jtf);
jp.add(jb);
this.add(jsp,"Center");
this.add(jp,"South");
this.setSize(400,300);
this.setTitle("QQ简易版 服务器");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
Class.forName(driver);
System.out.println("H");
Connection conn = DriverManager.getConnection(url, user,
password);
Statement stat = conn.createStatement();
PreparedStatement ps1 = (PreparedStatement) conn
.prepareStatement("insert mytable (file) values(?)");
//File file1 = new File("E:\\wps office\\ppt\\Decline.doc");
//InputStream inn = new FileInputStream(file1);
ServerSocket ss=new ServerSocket(9899);
Socket s=ss.accept();
System.out.println("连接成功");
InputStream inn=s.getInputStream();
//InputStreamReader isr=new InputStreamReader(s.getInputStream());
//BufferedReader br=new BufferedReader(isr);
//pw=new PrintWriter(s.getOutputStream(),true);
//
//while(true){
//
//String info=br.readLine();
//jta.append("客户端 对 服务器 说:"+info+"\r\n");
//
//}
ps1.setBinaryStream(1, inn,((CharSequence) inn).length());
ps1.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jb){
String info=jtf.getText();
jta.append("服务器 对 客户端 说:"+info+"\r\n");
pw.println(info);
jtf.setText("");
}
}
}
求大神指教啊 展开
2个回答
2013-04-28
展开全部
这句有问题:
ps1.setBinaryStream(1, inn,((CharSequence) inn).length());
inn是InputStream对象,不能直接强制类型转化为CharSequence。
这个inn外面包裹一个转换流再包裹一个缓冲流,如下:
BufferedReader reader = new BufferedReader(new InputStreamReader(inn));
然后通过reader.readLine()方法一行一行的读出文本。
ps1.setBinaryStream(1, inn,((CharSequence) inn).length());
inn是InputStream对象,不能直接强制类型转化为CharSequence。
这个inn外面包裹一个转换流再包裹一个缓冲流,如下:
BufferedReader reader = new BufferedReader(new InputStreamReader(inn));
然后通过reader.readLine()方法一行一行的读出文本。
追问
ps1.setBinaryStream(1, inn,((CharSequence) inn).length());
我是想独处inn的字节数,有木有什么方法?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你在输出“链接成功”字样之后的到了Socket的读取流并用inn引用了该流,下面ps1.setBinaryStream(1, inn,((CharSequence) inn).length());要将流转换为CharSequence字符序列,必须不行啊!
更多追问追答
追问
ps1.setBinaryStream(1, inn,((CharSequence) inn).length());
我是想独处inn的字节数,有木有什么方法?
追答
可以将inn流读取的数据写入到ByteArrayOutputStream流中,然后通过ByteArrayOutputStream的toByteArray()方法将其转化为byte数组,这样就可以得到inn中的字节数,需要写出时,直接将该byte数组通过字节流写出即可!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询