我送從Java客戶端程序的請求,Servlet的像發送下面,讀取一個文件,字符串,整數從由servlet的Java客戶端
URL url = new URL("http://localhost:8080/TestWebProject/execute");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("POST");
DataOutputStream out = new DataOutputStream(httpCon.getOutputStream());
out.writeUTF("hello");
out.writeUTF("World");
ByteArrayOutputStream bos;
File baseFile = new File("C:\\Users\\jp\\Desktop\\mdn.txt");
if (!baseFile.exists())
{
System.out.println("File Not Found Correctly");
}
FileInputStream fis = new FileInputStream(baseFile);
byte[] fileBytes = new byte[1024];
bos = new ByteArrayOutputStream();
while (true)
{
int ch = fis.read(fileBytes);
if (ch != -1)
{
bos.write(fileBytes, 0, ch);
}
else
{
break;
}
}
bos.close();
fis.close();
out.write(bos.toByteArray());
out.writeInt(10);
out.close();
* ** * * * * ** *SERVLET SIDE* ** * ** * ** *
InputStream is = (InputStream) req.getInputStream();
DataInputStream dis = new DataInputStream(is);
System.out.println("NAME US :" + dis.readUTF());
System.out.println("NAME US 1:" + dis.readUTF());
File f = new File("D:\\temp2.txt");
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
byte[] fileBytes = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while (true)
{
int ch = dis.read(fileBytes);
if (ch != -1)
{
bos.write(fileBytes, 0, ch);
}
else
{
break;
}
}
fos.write(bos.toByteArray());
System.out.println(dis.readInt());
我得到輸出 你好 世界 同樣的文件被成功複製到提到的位置temp2.txt
我在System.out.println(dis.readInt())中遇到問題;當EOF達到時。
我在哪裏做錯了,以及如何從DataInputStream中讀取數據。
謝謝。
錯誤堆棧跟蹤信息,請 – 2012-07-18 07:17:05
java.io.EOFException的 \t在java.io.DataInputStream中.readInt(DataInputStream.java:375) \t at TestServlet.doPost(TestServlet.java:59) \t at javax.servlet.http.HttpServlet.service(HttpServlet.j AVA:637) \t在javax.servlet.http.HttpServlet.service(HttpServlet.java:717) \t在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) \t在org.apache .catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) \t at – Jayesh 2012-07-18 07:59:53
您可以編輯帖子以添加更多詳細信息。不要粘貼錯誤,代碼註釋 – 2012-07-18 08:53:46