Good day.
I have a problem with running rest service from notes client.
I wrote method, which upload file to the server, using POST method.
When I click the button, which runs this metod first time everything works fine - file uploads: if I click this button one more time, service return HTTP Error 503: service Unavailable/
If I reopen client and click button everything is works, but if I click this button one more time, service return HTTP Error 503: service Unavailable.
Here is code :
public void AddFileToTable () {
try{
String repositoryId = “";
String binaryFilepath = “123.txt”;
String accessKey = "*”;
File binfile = new File(binaryFilepath);
if (!binfile.exists()) {
System.out.println(“File does not Exists”);
return;
};
String serviceURL1 = “";
String serverName = "”;
String serviceURL = serviceURL1+“/attmnt” +
“?repositoryId=” + repositoryId +
“&serverName=” + serverName +
“&mimeType=Text” +
“&description=” + binfile.getName();;
String payLoad = “”
- “<repository id="” + repositoryId + “">”
- “<orig_file_name>” + binfile.getName() + “</orig_file_name>”
- “<attmnt_name>” + binfile.getName() + “</attmnt_name>”
- “Uploaded using REST sample code”
- “”;
HttpURLConnection conn = null;
PrintWriter writer = null;
String newAttmntId = “”;
URL cc = new URL(serviceURL);
conn = (HttpURLConnection) cc.openConnection();
conn.setRequestMethod(“POST”);
// Set standard HTTP headers
conn.setRequestProperty(“Content-Type”,“multipart/form-data; BOUNDARY=” + MIME_MESSAGE_BOUNDARY);
// Set Access Key header
conn.setRequestProperty(“X-AccessKey”, accessKey);
conn.setDoOutput(true);
// Пеердаем парметры
OutputStream output = conn.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(output,
CHARACTERSET), true); // true = autoFlush, important!
// Send XML pay load
writer.append(“–” + MIME_MESSAGE_BOUNDARY).append(CRLF);
writer.append(“Content-Disposition: form-data; name="payload"”).append(CRLF);
writer.append(“Content-Type: application/xml; CHARACTERSET=” + CHARACTERSET).append(CRLF);
writer.append(CRLF);
writer.append(payLoad).append(CRLF).flush();
// Send binary file
writer.append(“–” + MIME_MESSAGE_BOUNDARY).append(CRLF);
writer.append(
“Content-Disposition: form-data; name="”
- binfile.getName() + “"; filename="”
- binfile.getName() + “"”).append(CRLF);
writer.append(“Content-Type: application/octet-stream”);
writer.append(CRLF);
// Note: In case of text files, following line can be omitted
writer.append("Content-Transfer-Encoding: " + TRANSFER_ENCODING).append(CRLF);
writer.append(CRLF).flush();
InputStream input = null;
input = new FileInputStream(binfile);
if (TRANSFER_ENCODING.equalsIgnoreCase(“base64”)) {
// Encode complete file with base64
long fileLen = binfile.length();
if (fileLen > Integer.MAX_VALUE) {
throw new IOException(“File is too large!”);
}
byte buffer = new byte[(int) fileLen];
int offset = 0;
int numRead = 0;
while (offset < buffer.length
&& (numRead = input.read(buffer, offset, buffer.length - offset)) >= 0) {
offset += numRead;
}
byte encBytes = Base64.encodeBase64(buffer);
output.write(encBytes, 0, encBytes.length);
} else {
byte buffer = new byte[1024];
for (int length = 0; (length = input.read(buffer)) > 0;) {
output.write(buffer, 0, length);
}
}
writer.append(CRLF).flush();
// End of multipart/form-data.
writer.append(“–” + MIME_MESSAGE_BOUNDARY + “–”).append(CRLF);
writer.flush();
// End of multipart/form-data.
writer.append(“–” + MIME_MESSAGE_BOUNDARY + “–”).append(CRLF);
writer.flush();
// Read Server response and write it to console
DataInputStream dataInStream = new DataInputStream(conn.getInputStream());
String str, responseString = “”;
while ((str = dataInStream.readLine()) != null) {
responseString += str;
}
dataInStream.close();
//System.out.println(“Response from server:”);
//System.out.println(responseString);
// Extract new attachment id from response. Note: 12 in following
// expression is number of characters in “<attmnt id="”
// Note: extracting id in this fashion is not always accurate
// It is safer to use xml parsers
int sIndex = responseString.indexOf(“<attmnt id="”) + 12;
int eIndex = responseString.indexOf(“"”, sIndex);
newAttmntId = responseString.substring(sIndex, eIndex);
System.out.println(newAttmntId);
}catch (Exception Err){
Err.printStackTrace();
}
}
The Error occures, when i trying to get Inputstream on line :
// Read Server response and write it to console
DataInputStream dataInStream = new DataInputStream(conn.getInputStream());
Can anybody helps.
Thanks .