java.lang.IllegalStateException: 请求数据已被读取 CQ5,AEM
java.lang.IllegalStateException: Request Data has already been read CQ5,AEM
我在 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)中用于读取 byte[] 的代码如下所示:
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 服务:未捕获的 Throwable
java.lang.IllegalStateException: 请求数据已被读取
除此错误外,我还收到以下错误,但我认为这无关紧要。
08.03.2016 15:17:31.092 ERROR [qtp87442412-7274] org.apache.sling.engine.impl.parameters.ParameterSupport getRequestParameterMapInternal: 错误解析请求
java.lang.IllegalArgumentException: 错误的转义序列: %6
我知道 request.getInputStream() 出现了一些问题,但不确定如何解决
我认为这是因为您缺少多部分信息,请查看 并更新您的代码,它应该可以解决问题。
好的,我通过另一种方式处理它来让它工作。现在我将 byte[] 作为字符串发送到参数中的 servlet,如下所示:
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("fileName", filename);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write("inputstream=" + Arrays.toString(input));
writer.close();
然后在 servlet 中读取参数如下:
String encodingScheme = "UTF-8";
request.setCharacterEncoding(encodingScheme);
String requestStr = request.getParameter("inputstream");
byte[] rawRequestMsg = requestStr.getBytes();
就像这样,我在 servlet 中获取字节 [],但这里的问题是,当我将此字节 [] 序列写入文件时,而不是写入内容,只写入字节,所以我只看到文件中的数字.我使用的写入文件的代码如下:
FileOutputStream fileOuputStream = new FileOutputStream(uploadedFileLocation);
fileOuputStream.write(byteArray); // byteArray is byte[]
fileOuputStream.close();
收到多部分消息时,Sling 和 HTTPServletRequest 背后的 servlet 实现将为您从输入流中解析出多部分参数,但同时会读取流。这使得 request.getInputStream();
不可用,但多部分对象将通过 request.getRequestParameterMap()
解析并可用。此处提供了有关处理多部分文件上传的有用 link:(编辑 - 新 link:Handling File Upload in CQ)
不是这个确切问题的答案,但是
当从 JS Ajax 发送 json 数据作为对 AEM servlet API.
的 POST 调用时,也会触发类似的异常
示例:将简单表单数据作为 JSON 发送到您的 AEM Servlet API。
当我们尝试使用 request.getInputStream() 时,我们得到相同的异常:
未捕获的 Throwable java.lang.IllegalStateException: 请求数据已被读取。
只需确保在 JS 中的 ajax 调用中,您发送
数据类型:“json”,
内容类型:“application/json”,
示例:
$.ajax({
type: "POST",
url: "/content/myproject/api/form-submit",
data : JSON.stringify(objData),
dataType: "json",
contentType: "application/json",
success: function (response) {
sucessHandler();
}
如果这些属性不存在,sling servlet 会以不同的方式处理原始数据,这有时也会触发相同的异常。
我在 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)中用于读取 byte[] 的代码如下所示:
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 服务:未捕获的 Throwable java.lang.IllegalStateException: 请求数据已被读取
除此错误外,我还收到以下错误,但我认为这无关紧要。
08.03.2016 15:17:31.092 ERROR [qtp87442412-7274] org.apache.sling.engine.impl.parameters.ParameterSupport getRequestParameterMapInternal: 错误解析请求 java.lang.IllegalArgumentException: 错误的转义序列: %6
我知道 request.getInputStream() 出现了一些问题,但不确定如何解决
我认为这是因为您缺少多部分信息,请查看 并更新您的代码,它应该可以解决问题。
好的,我通过另一种方式处理它来让它工作。现在我将 byte[] 作为字符串发送到参数中的 servlet,如下所示:
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("fileName", filename);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write("inputstream=" + Arrays.toString(input));
writer.close();
然后在 servlet 中读取参数如下:
String encodingScheme = "UTF-8";
request.setCharacterEncoding(encodingScheme);
String requestStr = request.getParameter("inputstream");
byte[] rawRequestMsg = requestStr.getBytes();
就像这样,我在 servlet 中获取字节 [],但这里的问题是,当我将此字节 [] 序列写入文件时,而不是写入内容,只写入字节,所以我只看到文件中的数字.我使用的写入文件的代码如下:
FileOutputStream fileOuputStream = new FileOutputStream(uploadedFileLocation);
fileOuputStream.write(byteArray); // byteArray is byte[]
fileOuputStream.close();
收到多部分消息时,Sling 和 HTTPServletRequest 背后的 servlet 实现将为您从输入流中解析出多部分参数,但同时会读取流。这使得 request.getInputStream();
不可用,但多部分对象将通过 request.getRequestParameterMap()
解析并可用。此处提供了有关处理多部分文件上传的有用 link:(编辑 - 新 link:Handling File Upload in CQ)
不是这个确切问题的答案,但是 当从 JS Ajax 发送 json 数据作为对 AEM servlet API.
的 POST 调用时,也会触发类似的异常示例:将简单表单数据作为 JSON 发送到您的 AEM Servlet API。
当我们尝试使用 request.getInputStream() 时,我们得到相同的异常: 未捕获的 Throwable java.lang.IllegalStateException: 请求数据已被读取。
只需确保在 JS 中的 ajax 调用中,您发送 数据类型:“json”, 内容类型:“application/json”,
示例:
$.ajax({
type: "POST",
url: "/content/myproject/api/form-submit",
data : JSON.stringify(objData),
dataType: "json",
contentType: "application/json",
success: function (response) {
sucessHandler();
}
如果这些属性不存在,sling servlet 会以不同的方式处理原始数据,这有时也会触发相同的异常。