我正在尝试创建一个文件上传网络服务,它还需要一些字符串作为参数作为我项目的一小部分传递......
到目前为止,我已经设法创建了一个文件上传网络服务。但它仅在我仅传递要上传的文件作为输入时才有效。现在我修改了网络服务以接受一些参数。但它给了我错误:
java.lang.IllegalArgumentException: URLDecoder: 转义 (%) 模式中的非法十六进制字符 - 对于输入字符串:“&'”
我在这里复制了整个堆栈跟踪,我觉得这对我毫无意义。这是我的网络服务:
@POST
@Path("/postdata")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void postData3(List<Attachment> atts, @FormParam("username") final String username, @Context HttpServletRequest request)
{
System.out.println(username);
String home = "/home/yashdosi/s";
for(Attachment att : atts)
{
DataHandler dataHandler = att.getDataHandler();
try
{
InputStream stream = dataHandler.getInputStream();
MultivaluedMap map = att.getHeaders();
OutputStream out = new FileOutputStream(new File(home + "//" + getFileName(map)));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = stream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
stream.close();
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是我的 Java 客户端:
HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httppost = new HttpPost("http://localhost:8080/fileupload-ws/services/postdata");
FileBody img = new FileBody(new File("/home/yashdosi/1.jpg"));
FileBody html = new FileBody(new File("/home/yashdosi/hotmail.html"));
StringBody contentBody = new StringBody("some.email@gmail.com");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("image", img);
reqEntity.addPart("html", html);
reqEntity.addPart("username", contentBody);
httppost.setEntity(reqEntity);
//httppost.setHeader("Content-Type", "multipart/form-data");
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println("Response content length: " + resEntity.getContentLength());
}
EntityUtils.consume(resEntity);
}
catch(Exception e)
{
e.printStackTrace();
}
finally {
try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {}
}
任何想法是什么导致了这个错误?或者我应该如何解决这个问题!?