conn = (HttpURLConnection) this.url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setConnectTimeout(100000000);
//Connection timeout is 10 seconds
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
conn.setRequestProperty("AccessKey", "logixerp");
}
// ordinary string data
private void writeStringParams() throws Exception {
Set keySet = textParams.keySet();
for (Iterator it = keySet.iterator(); it.hasNext();) {
String name = it.next();
String value = textParams.get(name);
ds.writeBytes("--" + boundary + "\r\n");
ds.writeBytes("Content-Disposition: form-data; name=\"" + name
+ "\"\r\n");
ds.writeBytes("\r\n");
ds.writeBytes(encode(value) + "\r\n");
}
}
// File data
private void writeFileParams() throws Exception {
Set keySet = fileparams.keySet();
for (Iterator it = keySet.iterator(); it.hasNext();) {
String name = it.next();
File value = fileparams.get(name);
ds.writeBytes("--" + boundary + "\r\n");
ds.writeBytes("Content-Disposition: form-data; name=\"" + name
+ "\"; filename=\"" + encode(value.getName()) + "\"\r\n");
ds.writeBytes("Content-Type: " + getContentType(value) + "\r\n");
ds.writeBytes("\r\n");
ds.write(getBytes(value));
ds.writeBytes("\r\n");
}
}
// Get the file upload type, the image format is image / png, image / jpg and so on. Non-image is application/octet-stream
private String getContentType(File f) throws Exception {
// return "application/octet-stream"; // This line is no longer subdivided into images, all as application/octet-stream types
ImageInputStream imagein = ImageIO.createImageInputStream(f);
if (imagein == null) {
return "application/octet-stream";
}
Iterator it = ImageIO.getImageReaders(imagein);
if (!it.hasNext()) {
imagein.close();
return "application/octet-stream";
}
imagein.close();
return "image/" + it.next().getFormatName().toLowerCase();
//Converts the value returned by FormatName to lowercase, defaults to uppercase
}
// Convert the file into a byte array
private byte[] getBytes(File f) throws Exception {
FileInputStream in = new FileInputStream(f);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = in.read(b)) != -1) {
out.write(b, 0, n);
}
in.close();
return out.toByteArray();
}
// Add end data
private void paramsEnd() throws Exception {
ds.writeBytes("--" + boundary + "--" + "\r\n");
ds.writeBytes("\r\n");
}
// Transcode a string containing Chinese, which is UTF-8. The server has to decode once
private String encode(String value) throws Exception{
return URLEncoder.encode(value, "UTF-8");
}
public static void main(String[] args) throws Exception {
AttachDocumentToWaybill2 u = new AttachDocumentToWaybill2(ACCESS_URL);
u.addFileParameter("file", new File(
"D:\\college doucument\\document\\New folder\\paper.pdf"));
u.addTextParameter("waybillNumber","DELHI13670");
u.addTextParameter("fileType","pdf");
byte[] b = u.send();
String result = new String(b);
System.out.println(result);
}
}