将带有POST请求的文件发送到Java Servlet服务

时间:2018-10-18 11:51:31

标签: java json post geoserver

我尝试将带有POST请求的JSON文件发送到我的地理服务器。在那里,我的jar文件作为OWS服务(Java Servlet)运行。

服务:

 public void upload(HttpServletRequest request,
                    HttpServletResponse response) throws ServletException, IOException {

    String fileName = request.getHeader("fileName");
    File saveFile = new File(SAVE_DIR + fileName);               
    InputStream inputStream = request.getInputStream();
    FileOutputStream outputStream = new FileOutputStream(saveFile);  
    byte[] buffer = new byte[BUFFER_SIZE];
    int bytesRead = -1;
    System.out.println("Receiving data...");     
    while ((bytesRead = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, bytesRead);
    }  
    System.out.println("Data received.");
    outputStream.close();
    inputStream.close();     
    System.out.println("File written to: " + saveFile.getAbsolutePath());   
    }
}

测试Java:

URL url = new URL("http://localhost:8080/geoserver/ows?request=upload&service=GeoRestTransfer&version=1.0.0");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("fileName", "test.json");
conn.setDoOutput(true);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
mapper.writeValue((OutputStream) wr, json); //JSONObject
wr.flush();
wr.close();
int responseCode = conn.getResponseCode();
System.out.println("Response Code : " + responseCode);

测试卷曲:

curl -d "@test.json" -X POST "http://localhost:8080/geoserver/ows?request=upload&service=GeoRestTransfer&version=1.0.0"

JSON文件 test.json

{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}

EDIT1:

新测试:

我使用conn.setRequestProperty("fileContent", jsonString);将标头中的相同JSON字符串发送到我的服务,读取该标头并将其写入我的文件中,然后它可以工作。但是为什么它与身体不起作用?

URL url = new URL("http://localhost:8080/geoserver/ows?request=upload&service=GeoRestTransfer&version=1.0.0");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("fileName", "test.json");
conn.setRequestProperty("fileContent", jsonString);
conn.setDoOutput(true);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
mapper.writeValue((OutputStream) wr, content);
wr.flush();
wr.close();
int responseCode = conn.getResponseCode();
System.out.println("Response Code : " + responseCode);

问题:

如果我使用Java或curl发送此POST请求,我总是会收到错误消息。

错误:

  

org.xmlpull.v1.XmlPullParserException:在开始标记之前仅允许空白内容,而不是{(位置:已看到START_DOCUMENT {... @ 1:1)

如果我尝试使用一个空的帖子正文,它将正常工作并创建一个空文件。 我可以像我的新测试一样发送带有标题的内容,这也可以工作。 是否有可能通过post将主体中的jsonobject发送到OWS服务?还是为什么我会收到此错误?

0 个答案:

没有答案