我需要通过网络发送一个字符串,并且我在Spring Boot的REST控制器中获取它。
我使用以下行发送数据:
byte[] content = sw.toString().getBytes(StandardCharsets.UTF_8); //sw is StringWriter
httpUrlConnection = (HttpURLConnection)new URL("http://127.0.0.1:8080/upload").openConnection();
httpUrlConnection.setDoInput(false);
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.setFixedLengthStreamingMode(content.length);
httpUrlConnection.connect();
httpUrlConnection.getOutputStream().write(content);
httpUrlConnection.getOutputStream().flush();
我收到这样的话:
@RequestMapping(value="/upload", method = RequestMethod.POST)
public ResponseEntity<String> upload(@RequestBody byte[] uploadedData)
throws NoSuchAlgorithmException, InvalidKeyException, IOException {
if(uploadedData == null) {
log.info("Uploaded data was null.");
return new ResponseEntity<String>("Data was null after upload.", HttpStatus.INTERNAL_SERVER_ERROR);
}
String receivedData = new String(uploadedData, StandardCharsets.UTF_8);
log.info("" + receivedData);
但我发送的内容却是......
-----BEGIN CERTIFICATE REQUEST-----
MIIB0DCCATkCAQAwgY8xCzAJBgNVBAYTAkFVMSgwJgYDVQQKDB9UaGUgTGVnaW9u
IG9mIHRoZSBCb3VuY3kgQ2FzdGxlMRIwEAYDVQQHDAlNZWxib3VybmUxETAPBgNV
BAgMCFZpY3RvcmlhMS8wLQYJKoZIhvcNAQkBFiBmZWVkYmFjay1jcnlwdG9AYm91
bmN5Y2FzdGxlLm9yZzCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAqE+pEyKS
21JXgeldS83E+SSmQ4s2xYPqV7Yw2ebUKsxnbBz1KcXHdHS1MJ9PYzBJgogdigZW
6hEQ83edP/Ay/EQzGqeKUzFqNEsQh3PSdbF9N5k7b81tQHUbfIbNu1ofSBNa/Eit
MkOj1NAmwivpW0AA8aPZhGzYLYWcp0lsC78CAwEAAaAAMA0GCSqGSIb3DQEBBQUA
A4GBAB0p0ySmfMkm3z8H4P8WwWJ8bMO2RNXEx0i9fU2ncJfdY0zEPYvM6zpUhwJP
T9DsQBPdSy+VLbJ/PtYoiKIcupd+vriGYn3mqckXy7RBLqpiVsnw1rGE28oG4I9N
u0p2AwDuC+KNuHgtrGxYrRnFTRKZpj2AoGuW1a6eSaNOhPeq
-----END CERTIFICATE REQUEST-----
我得到的是:
-----BEGIN+CERTIFICATE+REQUEST-----%0D%0AMIIB0DCCATkCAQAwgY8xCzAJBgNVBAYTAkFVMSgwJgYDVQQKDB9UaGUgTGVnaW9u%0D%0AIG9mIHRoZSBCb3VuY3kgQ2FzdGxlMRIwEAYDVQQHDAlNZWxib3VybmUxETAPBgNV%0D%0ABAgMCFZpY3RvcmlhMS8wLQYJKoZIhvcNAQkBFiBmZWVkYmFjay1jcnlwdG9AYm91%0D%0AbmN5Y2FzdGxlLm9yZzCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAqE+pEyKS%0D%0A21JXgeldS83E+SSmQ4s2xYPqV7Yw2ebUKsxnbBz1KcXHdHS1MJ9PYzBJgogdigZW%0D%0A6hEQ83edP%2FAy%2FEQzGqeKUzFqNEsQh3PSdbF9N5k7b81tQHUbfIbNu1ofSBNa%2FEit%0D%0AMkOj1NAmwivpW0AA8aPZhGzYLYWcp0lsC78CAwEAAaAAMA0GCSqGSIb3DQEBBQUA%0D%0AA4GBAB0p0ySmfMkm3z8H4P8WwWJ8bMO2RNXEx0i9fU2ncJfdY0zEPYvM6zpUhwJP%0D%0AT9DsQBPdSy+VLbJ%2FPtYoiKIcupd+vriGYn3mqckXy7RBLqpiVsnw1rGE28oG4I9N%0D%0Au0p2AwDuC+KNuHgtrGxYrRnFTRKZpj2AoGuW1a6eSaNOhPeq%0D%0A-----END+CERTIFICATE+REQUEST-----%0D%0A=
这不好,因为它会杀死PEM解析器。
但是,我可以通过使用以下转换来克服此问题,并且String变为可解析...
receivedData = receivedData.replace("-----BEGIN+CERTIFICATE+REQUEST-----", "-----BEGIN CERTIFICATE REQUEST-----");
receivedData = receivedData.replace("-----END+CERTIFICATE+REQUEST-----", "-----END CERTIFICATE REQUEST-----");
receivedData = receivedData.replace("%0D%0A", "\r\n");
receivedData = receivedData.replace("%2F", "/");
receivedData = receivedData.substring(0, receivedData.length()-1);
在哪种情况下解析成功...
2014-10-18 16:26:43.864 INFO 2432 --- [nio-8080-exec-1] demo.HelloController : PemParser returned: org.spongycastle.pkcs.PKCS10CertificationRequest@f5c81fb9
2014-10-18 16:26:43.870 INFO 2432 --- [nio-8080-exec-1] demo.HelloController : SUCCESS
...但我不认为这是一种非常可行的做事方式,因为它看起来并不稳定。一定有更好的方法。我尝试了URLEncoder.decode(receivedData, "UTF-8");
,但它从实际数据中删除了+
个字符,这也不好,因为它使数据无法解析。
当我通过网络发送URL escaping
时,有没有人对如何阻止此String
有任何有用的想法?
答案 0 :(得分:1)
好吧,我仍然不知道发生了为什么的答案,但我确实做了一个解决方法。
我不是仅仅发送String
byte [] 内容,而是根据https://jsonp.java.net/index.html将数据包装在http://docs.oracle.com/javaee/7/tutorial/doc/jsonp003.htm的JSON对象中。
JsonObject model = Json.createObjectBuilder().add("data", sw.toString()).build();
httpsUrlConnection = (HttpsURLConnection)new URL("http://127.0.0.1:8443/upload").openConnection();
httpsUrlConnection.setDoInput(true);
httpsUrlConnection.setDoOutput(true);
httpsUrlConnection.setRequestProperty("Content-Type", "application/json");
httpsUrlConnection.setRequestMethod("POST");
httpsUrlConnection.connect();
try(JsonWriter jsonWriter = Json.createWriter(httpsUrlConnection.getOutputStream()))
{
jsonWriter.write(model);
}
httpsUrlConnection.getOutputStream().flush();
System.out.println("Response code: " + httpsUrlConnection.getResponseCode());
之后,在Spring Boot端,我进行了设置,以便按照https://stackoverflow.com/a/8946142/2413303收到JSON对象:
public class DataObject {
private String data;
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
在控制器中:
@RequestMapping(value="/upload", method = RequestMethod.POST, consumes="application/json")
public ResponseEntity<String> upload(@RequestBody DataObject uploadedData)
throws NoSuchAlgorithmException, InvalidKeyException, IOException {
if(uploadedData == null) {
log.info("Uploaded data was null.");
return new ResponseEntity<String>("Data was null after upload.", HttpStatus.INTERNAL_SERVER_ERROR);
}
String receivedData = uploadedData.getData();
log.info("" + receivedData);
这样就不会转义字符:
2014-10-20 13:03:15.550 INFO 6924 --- [nio-8443-exec-3] demo.HelloController : -----BEGIN CERTIFICATE REQUEST-----
编辑:我被告知问题是没有设置内容类型的,Spring默认为application/x-www-form-urlencoded
,因为它应该是application/octet-stream
。
EDIT2:使用octet-stream
,通过转发\r
和\n
将其发送至\\r
和\\n
,并在之前添加"
由于某种原因在字符串之后。不知道为什么。