Index: lams_common/src/java/org/lamsfoundation/lams/util/WebUtil.java =================================================================== diff -u -r5773f84ed608838de3521ecde87c52f3c72d478c -r73468dfa4bf7c6242bc350c790374188b5b97646 --- lams_common/src/java/org/lamsfoundation/lams/util/WebUtil.java (.../WebUtil.java) (revision 5773f84ed608838de3521ecde87c52f3c72d478c) +++ lams_common/src/java/org/lamsfoundation/lams/util/WebUtil.java (.../WebUtil.java) (revision 73468dfa4bf7c6242bc350c790374188b5b97646) @@ -517,117 +517,6 @@ return is; } - /** - * Uploads a file to the given url. Uses a multi-part http post to post the file as well as the user, course, and - * hash server-authentication strings. - * - * Some of the java multipart posting libraries clashed with existing jboss libraries So instead, the multipart post - * is put together manually - * - * @param f - * @param urlString - * @param timestamp - * @param extUsername - * @param extCourseId - * @param hash - * @return - * @throws IOException - */ - public static InputStream performMultipartFilePost(File f, String fileParamName, String urlString, - HashMap params) throws MalformedURLException, IOException, Exception { - HttpURLConnection conn = null; - DataOutputStream dos = null; - String lineEnd = "\r\n"; - String twoHyphens = "--"; - String boundary = "*****"; - int bytesRead, bytesAvailable, bufferSize; - byte[] buffer; - int maxBufferSize = 1 * 1024 * 1024; - - // ------------------ CLIENT REQUEST - FileInputStream fileInputStream = new FileInputStream(f); - - WebUtil.log.info("Performing multipart post to: " + urlString); - - // open a URL connection to the Servlet - URL url = new URL(urlString); - - // Open a HTTP connection to the URL - conn = (HttpURLConnection) url.openConnection(); - - if (!(conn instanceof HttpURLConnection)) { - WebUtil.log.error("Fail to connect to external server though url: " + urlString); - throw new Exception("Fail to connect to external server though url: " + urlString); - } - - // Allow Inputs - conn.setDoInput(true); - - // Allow Outputs - conn.setDoOutput(true); - - // Don't use a cached copy. - conn.setUseCaches(false); - - String httpRequest = ""; - for (Entry entry : params.entrySet()) { - httpRequest += twoHyphens + boundary + lineEnd; - httpRequest += "Content-Disposition: form-data; name=\"" + entry.getKey() + "\";" + lineEnd; - httpRequest += lineEnd; - httpRequest += entry.getValue() + lineEnd; - } - httpRequest += twoHyphens + boundary + lineEnd; - httpRequest += "Content-Disposition: form-data; name=\"" + fileParamName + "\";" + " filename=\"" + f.getName() - + "\"" + lineEnd; - httpRequest += lineEnd; - - // Use a post method. - conn.setRequestMethod("POST"); - conn.setRequestProperty("Connection", "Keep-Alive"); - conn.setRequestProperty("Content-Length", new Long(f.length() + httpRequest.getBytes().length + 64).toString()); - WebUtil.log.debug(f.length()); - WebUtil.log.debug(httpRequest.getBytes().length); - WebUtil.log.debug("" + f.length() + httpRequest.getBytes().length + 64); - WebUtil.log.debug(httpRequest); - - conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); - - dos = new DataOutputStream(conn.getOutputStream()); - dos.writeBytes(httpRequest); - - // create a buffer of maximum size - bytesAvailable = fileInputStream.available(); - bufferSize = Math.min(bytesAvailable, maxBufferSize); - buffer = new byte[bufferSize]; - - // read file and write it into form... - bytesRead = fileInputStream.read(buffer, 0, bufferSize); - while (bytesRead > 0) { - dos.write(buffer, 0, bufferSize); - bytesAvailable = fileInputStream.available(); - bufferSize = Math.min(bytesAvailable, maxBufferSize); - bytesRead = fileInputStream.read(buffer, 0, bufferSize); - } - - // send multipart form data necessary after file data... - dos.writeBytes(lineEnd); - dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); - // Write the file part into the post------------------------------- - - // close streams - fileInputStream.close(); - dos.flush(); - dos.close(); - - InputStream ret = conn.getInputStream(); - if (ret == null) { - WebUtil.log.error("Fail to get response from extenal server, return InputStream null: " + urlString); - throw new Exception("Fail to fetch data from external server, return inputStream null: " + urlString); - } - - return conn.getInputStream(); - } - public static String extractParameterValue(String url, String param) { if (!StringUtils.isBlank(url) && !StringUtils.isBlank(param)) { int quotationMarkIndex = url.indexOf("?");