Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Java客户端发送的servlet中读取文件、字符串、Int_Java_Jsp_Jakarta Ee_Servlets_Servlet 3.0 - Fatal编程技术网

从Java客户端发送的servlet中读取文件、字符串、Int

从Java客户端发送的servlet中读取文件、字符串、Int,java,jsp,jakarta-ee,servlets,servlet-3.0,Java,Jsp,Jakarta Ee,Servlets,Servlet 3.0,我正在从java客户端程序向Servlet发送一个请求,如下所示 URL url = new URL("http://localhost:8080/TestWebProject/execute"); HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); httpCon.setDoOutput(true); httpCon.setRequestMethod("POST"); DataOutputStream o

我正在从java客户端程序向Servlet发送一个请求,如下所示

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端***********

我得到的输出是 你好 世界 此外,文件已成功复制到提到的位置temp2.txt

我在line System.out.println(dis.readInt())中遇到问题;当EOF到达时

我在哪里出错,以及如何从DataInputStream读取数据


谢谢。

您的循环循环直到您的流为“空”,然后退出循环。但之后,您尝试再次读取流,该流为空,您将达到eof。 您要打印到sysout的数据是什么

编辑

您可以直接写入
FileOutputStream
,然后像这样重写循环:

FileOutputStream fos=new FileOutputStream(f);

byte[] fileBytes=new byte[1024];
int ch;

while(((ch = dis.read(fileBytes)) != -1) {
   fos.write(fileBytes,0,ch);
}

错误堆栈跟踪pleasejava.io.EOFException在java.io.DataInputStream.readInt(DataInputStream.java:375)在TestServlet.doPost(TestServlet.java:59)在javax.servlet.http.HttpServlet.service(HttpServlet.java:637)在javax.servlet.http.HttpServlet.service(HttpServlet.java:717)在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)at您可以编辑帖子以添加更多详细信息。不要粘贴错误,注释中的代码我想发送数据,例如DataOutputStream out=newdataoutputstream(con.getOutputStream());out.writeUTF(“Hello”);out.write(bytes)//这里我将文件dat作为Byte array.out.writeUTF(“World”);现在,我想在Servlet端读取相同的内容。我能够读取DataInputStream dis=new DataInputStream(is);System.out.println(“NAME US:+dis.readUTF());dis.read()//System.out.println(“NAME US 1:+dis.readUTF());我能够读取第一个UTF和第二个写入文件。问题出现在第三个读取UTF中,因为它给出了EOF异常。在这种情况下如何读取数据?感谢我读取我的文件,就像这样:DataInputStream dis=new DataInputStream(is);System.out.println(“名称US:+dis.readUTF());System.out.println(“名称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());
FileOutputStream fos=new FileOutputStream(f);

byte[] fileBytes=new byte[1024];
int ch;

while(((ch = dis.read(fileBytes)) != -1) {
   fos.write(fileBytes,0,ch);
}