Hi ,
Really need some help here. I've been trying to upload a 1GB document via the ContenService.UploadContent and had the Java Out of Memory Error (Guess the heap space aint enough). I then trying to use the HTTP Content Handler(Almost identical to what is provided in the example) to try to upload the file but i get a 405 Error Code. I'm using CS10.5.
Anyone can help direct me why i'm getting this 405 error?
public long uploadDocument(long parentID, File f, String fName,String description)
{
AppLogUtil.log(Constants.LOG_LVL_DETAILED,"Parent ID is "+parentID);
String contextID = dm.createDocumentContext( parentID, fName ,description , false, null );
// JAX-WS doesn't handle the headers automatically, need to manually add
try{
setSoapHeader( (WSBindingProvider) cs, getOTAuth(), contextID, generateFAtts(f));
String dObjID=cs.uploadContent(new DataHandler( new FileDataSource( f )));
AppLogUtil.log(Constants.LOG_LVL_DETAILED,"uploaded document id is "+dObjID);
return Long.parseLong(dObjID);
}
catch(SOAPFaultException ee){
//verify if the document is already in the system
Node temp = dm.getNodeByName(parentID, fName);
if(temp!=null){
AppLogUtil.log(Constants.LOG_LVL_LOG_STEPS,"WARNING: File name exist in Folder, adding version to Document ID "+temp.getID());
try{
Version v=dm.addVersion(temp.getID(), null, setAttachment(f));
AppLogUtil.log(Constants.LOG_LVL_DETAILED,"added document ["+fName+"] to version("+v.getNumber()+") successfully");
return (int) temp.getID();
}
catch(SOAPFaultException dme)
{
AppLogUtil.log(Constants.LOG_LVL_ERR_ONLY,"Error while trying to add version :"+dme.getFault().getFaultCode()+" : "+dme.getFault().getFaultString());
}
catch(Exception e)
{
AppLogUtil.log(Constants.LOG_LVL_ERR_ONLY,"Error while trying to add version :"+e.getMessage());
}
}
}
catch(OutOfMemoryError ee)
{
AppLogUtil.log(Constants.LOG_LVL_DETAILED,"File too large to be uploaded via Content Service , using Http POST instead");
try
{
// Unable to transfer file due to large file size. Using a Post Instead
URL uri = constructURL(contextID, fName, f.length());
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application/octet-stream");
conn.setRequestProperty("Content-Length", String.valueOf(f.length()));
//conn.setFixedLengthStreamingMode((int)f.length());
conn.setChunkedStreamingMode(BUFFER_SIZE);
conn.connect();
AppLogUtil.log(Constants.LOG_LVL_DETAILED,"HTTP POST Connection Established");
//Stream Document file
FileInputStream fs = new FileInputStream(f);
OutputStream uploadStream = conn.getOutputStream();
int bytesRead = 0;
byte[] buf = new byte[BUFFER_SIZE];
while ((bytesRead = fs.read(buf))>0)
{
uploadStream.write(buf, 0, bytesRead);
uploadStream.flush();
}
fs.close();
uploadStream.close();
// read the response from the Content Handler
BufferedInputStream inStream = new BufferedInputStream(conn.getInputStream());
String dObjID = parseResponse(inStream);
inStream.close();
if(dObjID != null)
{
AppLogUtil.log(Constants.LOG_LVL_DETAILED,"File Uploaded Successfully.ID is ["+dObjID+"]. HTTP POST Connection Terminated");
return Long.parseLong(dObjID);
}
}
catch(Exception e)
{
AppLogUtil.log(Constants.LOG_LVL_ERR_ONLY,"Error while using Content Handler to upload document: "+e.getMessage());
}
}
catch(Exception e)
{
AppLogUtil.log(Constants.LOG_LVL_ERR_ONLY,"Error detected while uploading document: "+e.getMessage());
}
return 0;
}
private URL constructURL( String contextID, String fName, long fileSize) throws Exception
{
String uri = getContentHandler() + "?token=" + URLEncoder.encode(getToken(),"utf-8") + "&contextID=" + contextID;
if( fName != null)
{
uri += "&fileName=" + fName + "&fileSize=" + String.valueOf(fileSize);
}
return new URL(uri);
}
private String parseResponse(InputStream content)
{
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
String dObjID = null;
try{
DocumentBuilder builder = f.newDocumentBuilder();
Document d = builder.parse(content);
NodeList list = d.getChildNodes();
for( int i=0; i< list.getLength(); i++)
{
if("objectID".equals(list.item(i).getNodeName()))
{
dObjID= list.item(i).getTextContent();
}
}
}
catch (Exception e)
{
AppLogUtil.log(Constants.LOG_LVL_ERR_ONLY,"Error detected while uploading document (Via HTTP Content Handler):"+e.getMessage());
}
return dObjID;
}
Error returned:
Server returned HTTP response code: 405 for URL: http://enterprise/cws/HttpContentHandler.ashx?token=BSiOlPMA50dlrt2phAJh%252F9G74%252Fr%252FbGE%252Bn%252BGOZnDmgKy9Z%252FNt1rfhrHN496SDlEzP&contextID=476616981&fileName=300.pdf&fileSize=824089973
Thank you very very very much.
regards
Wilson