这是我的服务器代码?是有问题的。
package nbx.com;import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import nbx.db.com.DataBase;
import nbx.pact.com.Pact;public class SendPF extends HttpServlet implements Servlet { public SendPF() {
super();
}
public void destroy() {
super.destroy();
}

public void process(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/octet-stream");
final InputStream is = request.getInputStream();
final OutputStream os = response.getOutputStream();
final DataInputStream dis = new DataInputStream(is);
final DataOutputStream dos = new DataOutputStream(os);
;
int action = dis.readInt();
if(action == Pact.UPLOAD){//如果接收到的信号是准备上传,就执行uploadPF方法;
this.uploadPF(dis,is,dos);
}
}
  
private void uploadPF(DataInputStream dis, InputStream is, DataOutputStream dos) {
try {

int size = 0;
byte[] b = new byte[100000];
byte[] tmp = new byte[100000];
long time = System. currentTimeMillis();//系统时间 ;
//创建一个文件夹用来保存发过来的图片;
File f = new File("D:/tomcat6.0/Tomcat6.0官方版/tomcat6.0.14/webapps/nabaixinServer1.0/pictures/" + time + ".png");
dos = new DataOutputStream(new FileOutputStream(f));
int len = 0;
while( ( len = is.read( tmp ) ) != -1 )

dos.write(tmp,0,len);
size+=len;
}
dos.close();
String username = dis.readUTF();//出错。
String textFile = dis.readUTF();
String picture = f.getName();
if(checkPF(textFile)){
dos.writeInt(Pact.UPLOADOK);
uploadPFOK(username,textFile,picture);  
dos.close();
}else{
dos.writeInt(Pact.UPLOADFILL);
dos.close();
}


} catch (IOException e) {
e.printStackTrace();
}
}
private void uploadPFOK(String username, String textFile, String picture) {
DataBase db = new DataBase();
String sqlStm = "insert into picture (username,connect,picture) values('"+username+"','"+textFile+"','"+picture+"')";
db.update(sqlStm);

}
private boolean checkPF(String textFile) {
boolean result = false; 
DataBase db = new DataBase();
String sqlStm = "select * from file where connect ='" + textFile+"'";
ResultSet rs = db.query(sqlStm);
try {
if(rs.next()){
System.out.println("此方法运行了!");
return result;
}else{
result = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
process(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
process(request,response); } public void init() throws ServletException {
}}
下面是客户端程序代码,没有问题的。给大虾们做查看用。
package nbx.com;import java.io.DataInputStream;
import java.io.DataOutputStream;import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Form;import nbx.pact.com.Pact;public class HttpSendPF {
private String spfURL = "localhost:8080/nabaixinServer1.0/sendpf";//登录时服务器的URL;
private HttpConnection hcon = null; public void sendPF(String name, String text, byte[] picture, Form sendPF) {
try{
hcon = (HttpConnection) Connector.open("http://" + spfURL );//与服务器建立连接;
DataOutputStream dos = hcon.openDataOutputStream();
dos.writeInt(Pact.UPLOAD);//向服务器发送准备上传文件、图片信号;
dos.writeUTF(name);//向服务器发送登录者的用户名;
dos.writeUTF(text);//向服务器发送文件信息;
dos.write(picture,0,picture.length);//向服务器发送图片信息;
dos.close();//关闭输出流;
System.out.println("用户名"+name);
System.out.println("文本内容"+text);
System.out.println("图片"+picture);
DataInputStream dis = hcon.openDataInputStream();//打开输入流;
if(dis.readInt() == Pact.UPLOADFILL){
Alert alt=new Alert("提示","文件已保存,请保存其它文件!",null,AlertType.ERROR);
alt.setTimeout(4000);
Client.dp.setCurrent(alt,sendPF);
}else{
System.out.println(" Congratulations on a successful " + text + " upload ");
System.out.println(" Congratulations on a successful " + picture + " upload ");
}
}catch(Exception e){
e.printStackTrace();
}
}}兄弟我很急,望出手帮忙!

解决方案 »

  1.   

    楼主问题解决了吗?共享一下。
    如果没有,我在调试你的代码的时候,发现没有你自己的在
    服务器:
    import nbx.db.com.DataBase; 
    import nbx.pact.com.Pact; 
    客户端:
    import nbx.pact.com.Pact; 
    包。能把它们都贴出来更好。
      

  2.   

    读写顺序要一致客户端写入顺序是:
    dos.writeInt(Pact.UPLOAD);//向服务器发送准备上传文件、图片信号;
    dos.writeUTF(name);//向服务器发送登录者的用户名;
    dos.writeUTF(text);//向服务器发送文件信息;
    dos.write(picture,0,picture.length);//向服务器发送图片信息;
    dos.close();//关闭输出流;那么服务器的读取顺序也应该是
    readInt();
    readUTF();
    readUTF();
    read(...)看下楼主自己的代码
    明显顺序错了