我在java中有一个代码,它使用POST将byte []发送到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());
用于读取byte []的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 ERROR [127.0.0.1 [1457430567960] POST / bin / services / updateslafile HTTP / 1.1] org.apache.sling.engine.impl.SlingRequestProcessorImpl service:未捕获的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:错误的转义序列:%ۑ
我知道request.getInputStream()提出了一些问题但不确定如何解决它
答案 0 :(得分:3)
当接收到多部分消息时,Sling和HTTPServletRequest之后的servlet实现将为您解析输入流中的多部分参数,但是会在这样做时读取流。这使得request.getInputStream();
无法使用,但多部分对象将通过request.getRequestParameterMap()
进行解析并可用。有关处理多部分文件上传的有用链接,请访问:(编辑 - 新链接:Handling File Upload in CQ)
答案 1 :(得分:0)
我认为这是因为您缺少多部分信息,请查看Sending files using POST with HttpURLConnection并更新您的代码,它应该可以解决问题。
答案 2 :(得分:0)
好吧,我通过另一种方式处理它让它工作。现在我将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中获取byte []但是这里的问题是当我将这个byte []序列写入文件时,而不是写入内容只写入字节,所以我只看到文件中的数字。我在文件中写的代码如下:
FileOutputStream fileOuputStream = new FileOutputStream(uploadedFileLocation);
fileOuputStream.write(byteArray); // byteArray is byte[]
fileOuputStream.close();