java.lang.IllegalStateException:请求数据已被读取CQ5,AEM

java.lang.IllegalStateException:请求数据已被读取CQ5,AEM,aem,sling,Aem,Sling,我有一个java代码,它使用POST将字节[]发送到CQ servlet。发送的代码为: URL url = new URL("http://localhost:4503/bin/services/updateslafile"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); String authStr

我有一个java代码,它使用POST将字节[]发送到CQ servlet。发送的代码为:

        URL url = new URL("http://localhost:4503/bin/services/updateslafile");   
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();                    
        String authStr = "admin:admin";
        // encode data on your side using BASE64
        byte[] bytesEncoded = Base64.encodeBase64(authStr.getBytes());
        String authEncoded = new String(bytesEncoded);                                                                          
        connection.setRequestProperty("Authorization", "Basic "+authEncoded);
        connection.setDoOutput(true);      
        connection.setRequestMethod("POST");   
        connection.setRequestProperty("fileName", "test.docx");
            byte[] input;//assume input initialized with some .docx file content as byte[]
        OutputStream outs = connection.getOutputStream();
        outs.write(input);
        outs.flush();   
        outs.close();

        //for response reading
        StringBuffer strBuffer = new StringBuffer();
        InputStream inputStream = connection.getInputStream();
        byte[] b = new byte[1024];
        while ( is.read(b) != -1)
            strBuffer.append(new String(b));
        System.out.println("strbuffer : "+strBuffer.toString());
servlet(扩展SlingAllMethodsServlet)中用于读取字节[]的代码如下所示:

            String fileName = request.getHeader("fileName");
            // opens input stream of the request for reading data

           InputStream inputStream = request.getInputStream();// This line giving error
            String filePath = "/home/usr/documents/"+fileName;
            // opens an output stream for writing file
            FileOutputStream fileOuputStream = new FileOutputStream(filePath); 

            byte[] buffer = new byte[BUFFER_SIZE];
            int bytesRead = -1;
            LOGGER.info("Receiving data...");
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                     fileOuputStream.write(buffer, 0, bytesRead);
            }
            LOGGER.info("Data received.");
            fileOuputStream.close();
            inputStream.close();
现在,当我运行错误日志中的代码时,我得到了一些错误

08.03.2016 15:19:37.162错误[127.0.0.1[1457430567960]POST/bin/services/updateslafile HTTP/1.1]org.apache.sling.engine.impl.SlingRequestProcessorImpl服务:未捕获可丢弃 java.lang.IllegalStateException:已读取请求数据

除了这个错误,我也得到下面的错误,但我认为这是不相关的

08.03.2016 15:17:31.092错误[qtp87442412-7274]org.apache.sling.engine.impl.parameters.parameters支持getRequestParameterMapInternal:解析请求时出错 java.lang.IllegalArgumentException:错误的转义序列:%ۑ


我知道request.getInputStream()出现了一些问题,但不确定如何修复它

我认为这是因为您缺少多部分信息,请查看并更新您的代码,它可能会解决问题。

好的,我通过另一种方式处理它,使其正常工作。现在,我将字节[]作为字符串发送到参数中的servlet,如下所示:

connection.setDoOutput(true);
connection.setRequestMethod(“POST”);
setRequestProperty(“文件名”,文件名);
OutputStreamWriter writer=新的OutputStreamWriter(connection.getOutputStream());
writer.write(“inputstream=“+Arrays.toString(input));

writer.close()当接收到多部分消息时,Sling和HTTPServletRequest后面的servlet实现将为您解析输入流中的多部分参数,但在这样做时将读取流。这使得
请求.getInputStream()不可用,但将通过
request.getRequestParameterMap()
解析多部分对象并使其可用。这里提供了一个关于处理多部分文件上载的有用链接:(编辑-新建链接:)

实际上,我并没有将文件作为附件发送,因此即使我指定了多部分,它也不会工作。这是典型的CQ问题,与普通JavaServlet无关,但特定于CQ5Servlet。我发送的数据是字节[]的序列。