我正在尝试在我的基于 java 的应用程序中实现一种方法,该方法涉及将 zip 文件上传到我的服务器。客户端是java,服务器是java(REST,jboss 7)。过去我成功地上传了图像文件,但现在,使用 zip 文件时我遇到了问题,我的主要疑问是这些问题是与客户端相关还是与服务器相关(或两者兼而有之)。
所以,我的客户看起来像这样
final HttpHeaders headers = HttpClientUtils.headersJSONAndAcceptJSON();
MultiValueMap<String, Object> requestMap = new LinkedMultiValueMap<String, Object>();
addMap("filename", filename, requestMap);
addMap("contenttype", contentType, requestMap);
addMap("type", type, requestMap);
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
int b = -1;
//file data is the inputstream created from the File
while ( (b = filedata.read())>= 0 ) {
bout.write(b);
}
ByteArrayResource rs = new ByteArrayResource( bout.toByteArray() ){
@Override
public String getFilename() {
return "";
}
};
addMap("resource", rs, requestMap);
} catch (IOException e1) {
throw new IllegalStateException("Error");
}
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.setAccept(Arrays.asList(HttpClientUtils.mtypeJSONUtf8()));
final String url = this.baseURL + summaryURL;
try {
ResponseEntity<Summary> rEntity = restTemplate.exchange(
url,
HttpMethod.POST,
HttpClientUtils.entity(headers, requestMap),
Summary.class
(...)
同时在服务器端我有
@POST
@Path("/")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("application/json; charset=UTF-8")
public Summary addImportedSummary(@MultipartForm FileUploadFormObj imp)
{
Summary importedSummary = new Summary();
Map<String , String> newpath = new HashMap<String, String>();
if(imp.getFileData() != null)
{
ZipInputStream zip = new ZipInputStream(imp.getFileData());
ZipEntry entry;
try {
while ((entry = zip.getNextEntry()) != null)
{
if(entry.getName().endsWith(".html") || entry.getName().endsWith(".htm"))
{
if(entry.getSize() > 0)
{
StringWriter writer = new StringWriter();
IOUtils.copy(zip, writer, "UTF-8");
String content = writer.toString();
//do something with the content
}
}
}
zip.close();
} catch (IOException e) {
throw new BadRequestException("Error " + e);
}
}
当我尝试使用 IOUtils 或任何其他阅读器复制文件内容时,就会出现问题。我总是得到例外
ZipException too many length or distance symbols
现在,我认为问题可能出在我发送数据的方式上,因为文件是 zip,但我不知道问题出在哪里。大家有没有遇到过类似的问题?