xlsx的多部分帖子通过HTTPUrlConnection损坏

时间:2017-05-04 06:29:53

标签: java httpurlconnection multipartform-data xlsx

我试图通过HTTPUrlconnection发布xlsx文件,在接收端我得到了文件但是在MS Excel中打开它时说文件已损坏并需要修复。我的多部分帖子的代码片段

class MultipartUtility {
        private  final Logger log = getLogger(MultipartUtility.class.getName());
        private static final String CRLF = "\r\n";
        private static final String CHARSET = "UTF-8";
        private static final int CONNECT_TIMEOUT = 1500000;
        private static final int READ_TIMEOUT = 1000000;
        private final HttpURLConnection connection;
        private final OutputStream outputStream;
        private final PrintWriter writer;
        private final String boundary;
        // for log formatting only
        private final URL url;
        private final long start;

        public MultipartUtility(final String strUrl) throws IOException {
            start = currentTimeMillis();
            URL url  = new URL(strUrl);
            this.url = url;
            boundary = "---------------------------" + currentTimeMillis();
            connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(CONNECT_TIMEOUT);
            connection.setReadTimeout(READ_TIMEOUT);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Accept-Charset", CHARSET);
            connection.setRequestProperty("Content-Type","multipart/form-data; boundary=" + boundary);
            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            outputStream = connection.getOutputStream();
            writer = new PrintWriter(new OutputStreamWriter(outputStream, CHARSET),true);
        }
        public void addFilePart(final String  filePath)throws IOException {
            String fieldName = "content";
            File uploadFile = new File(filePath);
            final String fileName = uploadFile.getName();
            writer.append("--").append(boundary).append(CRLF)
                    .append("Content-Disposition: form-data; name=\"")
                    .append(fieldName).append("\"; filename=\"").append(fileName)
                    .append("\"").append(CRLF).append("Content-Type: ")
                    .append(guessContentTypeFromName(fileName)).append(CRLF)
                    .append("Content-Transfer-Encoding: binary").append(CRLF)
                    .append(CRLF);

            writer.flush();
            outputStream.flush();
            try (final FileInputStream inputStream = new FileInputStream(uploadFile);) {
                final byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
                outputStream.flush();
            }
            writer.append(CRLF);
        }


        public HashMap<Object, Object> finish() throws IOException {
            writer.append(CRLF).append("--").append(boundary).append("--").append(CRLF);
            writer.close();

            final int status = connection.getResponseCode();
            if (status != HTTP_OK) {
                throw new IOException(format("{0} failed with HTTP status: {1}",url, status));
            }
            try (final InputStream is = connection.getInputStream()) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                String sResponse;
                StringBuilder responseBuilder = new StringBuilder();
                while ((sResponse = reader.readLine()) != null) {
                    responseBuilder = responseBuilder.append(sResponse);
                }
                HashMap respMap = new HashMap();
                respMap.put("RESP_MSG", responseBuilder.toString());
                respMap.put("RESP_CODE", status);
                respMap.put("RESP_SIZE", responseBuilder.toString().length());
                log.log(INFO,format("{0} took {4} ms", url,(currentTimeMillis() - start)));
                log.log(INFO,"status::::::"+status);
                return respMap;
            } finally {
                connection.disconnect();
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

我尝试执行您的代码,并能够从java程序成功上传文件。 您报告的问题是我猜是由于文件的内容类型。如果您尝试上传.xlsx(MS Excel 2007),它会导致数据变形并需要在我们读取上传文件之前恢复。

如果您尝试上传.xls文件,它会正常上传而不会发生任何变形,MS excel会打开此文件而不会出现任何警告/错误。

所以我建议玩一下 writer.append(                 &#34;内容类型:&#34;                         +&#34; application / x-excel&#34;)

找到正确的内容类型参考: https://www.codeproject.com/Questions/481262/contentplustypeplusforplusxlsxplusfile

祝你好运

答案 1 :(得分:0)

问题出在您的 addFilePart 方法上,最后,您要附加“ writer.append(CRLF)” 当文件数为1时,您不应在末尾添加该文件,因为它将通过网络发送给您的文件大小增加2位,这会导致您的xlxs文件损坏。

如果要添加一个或多个文件,下面的

是代码。 在这种情况下,我们将CRLF(用于分隔文件)附加到writer到除最后一个文件之外的所有文件上。

df['Effective_date'] = pd.to_datetime(df['Effective_date'], format= '%d/%m/%Y')
df['Paid_Off_Time'] = pd.to_datetime(df['Paid_Off_Time'], format= '%d/%m/%Y')
for i in range(0,len(df))
  if df['Effective_Date'][i]>df['Paid_Off_Time'][i]:
    k=df['Effective_Date'][i]
    df['Effective_Date'][i]=df['Paid_Off_Time'][i]
    df['Paid_Off_Time'][i]=k