List of usage examples for java.io IOException toString
public String toString()
From source file:com.swetha.easypark.EasyParkHttpClient.java
/** * @param url of the website and the post request * @return http response in string */ public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception { BufferedReader in = null;/* ww w. j av a2 s. c om*/ try { HttpClient client = getHttpClient(); HttpPost request = new HttpPost(url); UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity( postParameters); request.setEntity(formEntity); Log.i("CustomHttpClient", "before sending request" + request); HttpResponse response = client.execute(request); Log.i("CustomHttpClient", "after sending request" + response.toString()); //Log.i("CustomHttpClient", "after sending request response in to string" +response.getEntity().getContent().toString()); in = new BufferedReader(new InputStreamReader(response.getEntity() .getContent())); StringBuffer sb = new StringBuffer(""); String line = ""; String NL = System.getProperty("line.separator"); while ((line = in.readLine()) != null) { sb.append(line + NL); } in.close(); String result = sb.toString(); return result; } finally { if (in != null) { try { in.close(); } catch (IOException e) { Log.e("log_tag", "Error converting result " + e.toString()); e.printStackTrace(); } } } }
From source file:eu.digitisation.idiomaident.utils.CorpusFilter.java
public static void filter(File in, File out, String refLanguage) { actualFile = new HashMap<>(); //first obtain the list of language folders folderList = getFolderList(in);// w w w . j a va2 s . co m //foreach file in the folder of the reference language //Last, we search for this word in the others language and if occurs in almost all //remove all occurrences (personal names). File refLangFolder = folderList.get(refLanguage); File[] filesInFolder = refLangFolder.listFiles(new FilenameFilter() { @Override public boolean accept(File file, String name) { return name.endsWith(".txt"); } }); int num = 0; for (File textFile : filesInFolder) { String fileName = textFile.getName(); actualFile.clear(); num++; System.out.println("Processing " + num + " of " + filesInFolder.length); try { //First we populate the actualFile hashmap with the same file //of the rest of languages, normalizing the text and removing the //special characters and the xml tags. for (String language : folderList.keySet()) { File languageFile = new File(folderList.get(language).getAbsolutePath() + "/" + fileName); if (languageFile.exists()) { String text = FileUtils.readFileToString(languageFile, Charset.forName("UTF-8")); text = StringNormalize.stringNormalize(text); actualFile.put(language, text); } } } catch (IOException ex) { System.err.println("Error: " + ex.toString()); } //Then we search in the text of the reference language for words with the //first caracter in uppercase. HashSet<String> posibleNames; posibleNames = getPosibleNames(actualFile.get(refLanguage)); //System.out.println(posibleNames.toString()); eliminateNames(posibleNames, actualFile); for (String lang : actualFile.keySet()) { try { //Copy the filter file in out folder FileUtils.writeStringToFile(new File(out.getAbsolutePath() + "/" + lang + "/" + fileName), actualFile.get(lang), Charset.forName("UTF-8")); } catch (IOException ex) { System.err.println("Error: " + ex.toString()); } } } }
From source file:gov.nist.appvet.tool.synchtest.util.ReportUtil.java
/** * This method returns report information to the AppVet ToolAdapter as ASCII * text and cannot attach a file to the response. *///from w w w. j a va 2 s.co m public static boolean sendInHttpResponse(HttpServletResponse response, String reportText, ToolStatus reportStatus) { try { response.setStatus(HttpServletResponse.SC_OK); // HTTP 200 response.setContentType("text/html"); response.setHeader("apprisk", reportStatus.name()); PrintWriter out = response.getWriter(); out.println(reportText); out.flush(); out.close(); log.info("Returned report"); return true; } catch (IOException e) { log.error("Report not sent: " + e.toString()); return false; } }
From source file:com.savy3.util.MainframeFTPClientUtils.java
public static List<String> listSequentialDatasets(String pdsName, Configuration conf) throws IOException { List<String> datasets = new ArrayList<String>(); FTPClient ftp = null;//w ww . ja va 2s .co m try { ftp = getFTPConnection(conf); if (ftp != null) { ftp.changeWorkingDirectory("'" + pdsName + "'"); FTPFile[] ftpFiles = ftp.listFiles(); for (FTPFile f : ftpFiles) { if (f.getType() == FTPFile.FILE_TYPE) { datasets.add(f.getName()); } } } } catch (IOException ioe) { throw new IOException("Could not list datasets from " + pdsName + ":" + ioe.toString()); } finally { if (ftp != null) { closeFTPConnection(ftp); } } return datasets; }
From source file:gov.nih.nci.ncicb.tcga.dcc.qclive.common.util.FileCopier.java
/** * Copies the contents of a file into a new file in the given directory. The new file will have the same name as * the original file.//from w w w . j a va 2 s.co m * * @param fromFile the file to copy * @param deployDirectory the directory in which to copy the file -- if this is not a directory, the new file will * be created with this name * * @return the File that was created * * @throws IOException if there are I/O errors during reading or writing */ public static File copy(final File fromFile, final File deployDirectory) throws IOException { // if not a directory, then just use deployDirectory as the filename for the copy File destination = deployDirectory; if (deployDirectory.isDirectory()) { // otherwise use original file's name destination = new File(deployDirectory, fromFile.getName()); } FileInputStream fileInputStream = null; FileChannel sourceChannel = null; FileOutputStream fileOutputStream = null; FileChannel destinationChannel = null; try { //noinspection IOResourceOpenedButNotSafelyClosed fileInputStream = new FileInputStream(fromFile); //noinspection ChannelOpenedButNotSafelyClosed sourceChannel = fileInputStream.getChannel(); //noinspection IOResourceOpenedButNotSafelyClosed fileOutputStream = new FileOutputStream(destination); //noinspection ChannelOpenedButNotSafelyClosed destinationChannel = fileOutputStream.getChannel(); final int maxCount = (64 * 1024 * 1024) - (32 * 1024); final long size = sourceChannel.size(); long position = 0; while (position < size) { position += sourceChannel.transferTo(position, maxCount, destinationChannel); } } catch (IOException ie) { throw new IOException("Failed to copy " + fromFile + " to " + deployDirectory + ".\n Error Details: " + ie.toString()); } finally { IOUtils.closeQuietly(fileInputStream); IOUtils.closeQuietly(sourceChannel); IOUtils.closeQuietly(fileOutputStream); IOUtils.closeQuietly(destinationChannel); } return destination; }
From source file:io.github.retz.web.JobRequestRouter.java
public static boolean statHTTPFile(String url, String name) { String addr = url.replace("files/browse", "files/download") + "%2F" + maybeURLEncode(name); HttpURLConnection conn = null; try {//from w w w . j a v a 2 s. c om conn = (HttpURLConnection) new URL(addr).openConnection(); conn.setRequestMethod("HEAD"); conn.setDoOutput(false); LOG.debug(conn.getResponseMessage()); return conn.getResponseCode() == 200 || conn.getResponseCode() == 204; } catch (IOException e) { LOG.debug("Failed to fetch {}: {}", addr, e.toString()); return false; } finally { if (conn != null) { conn.disconnect(); } } }
From source file:gov.nist.appvet.tool.synchtest.util.ReportUtil.java
/** * This method returns report information to the AppVet ToolAdapter as ASCII * text and cannot attach a file to the response. *//*from ww w .j a v a 2 s. co m*/ public static boolean sendPDFInHttpResponse(HttpServletResponse response, String reportText, ToolStatus reportStatus) { try { File reportFile = new File(reportFilePath); String mimeType = new MimetypesFileTypeMap().getContentType(reportFile); log.debug("Sending mimetype: " + mimeType); response.setContentType(mimeType); response.setHeader("Content-Disposition", "attachment;filename=" + reportFile.getName()); response.setStatus(HttpServletResponse.SC_OK); // HTTP 200 response.setHeader("apprisk", reportStatus.name()); FileInputStream inputStream = new FileInputStream(reportFile); //read the file try { int c; while ((c = inputStream.read()) != -1) { response.getWriter().write(c); } } finally { if (inputStream != null) inputStream.close(); response.getWriter().close(); } // PrintWriter out = response.getWriter(); // out.println(reportText); // out.flush(); // out.close(); log.info("Returned report"); return true; } catch (IOException e) { log.error("Report not sent: " + e.toString()); return false; } }
From source file:Main.java
/** * Make a URL from the given string./* w w w. j a va 2 s.c om*/ * * <p> * If the string is a properly formatted file URL, then the file * portion will be made canonical. * * <p> * If the string is an invalid URL then it will be converted into a * file URL. * * @param urlspec The string to construct a URL for. * @param relativePrefix The string to prepend to relative file * paths, or null to disable prepending. * @return A URL for the given string. * * @throws MalformedURLException Could not make a URL for the given string. */ public static URL toURL(String urlspec, final String relativePrefix) throws MalformedURLException { urlspec = urlspec.trim(); URL url; try { url = new URL(urlspec); if (url.getProtocol().equals("file")) { url = makeURLFromFilespec(url.getFile(), relativePrefix); } } catch (Exception e) { // make sure we have a absolute & canonical file url try { url = makeURLFromFilespec(urlspec, relativePrefix); } catch (IOException n) { // // jason: or should we rethrow e? // throw new MalformedURLException(n.toString()); } } return url; }
From source file:com.tacitknowledge.util.discovery.ClasspathUtils.java
public static String getCanonicalPath(String path) { File file = new File(path); String canonicalPath = null;/*from ww w . j ava 2s.com*/ if (file.exists()) { try { canonicalPath = file.getCanonicalPath(); } catch (IOException e) { log.warn("Error resolving filename to canonical file: " + e.toString()); } } if (canonicalPath == null) { canonicalPath = file.getPath(); } return canonicalPath; }
From source file:com.csc.fi.ioapi.utils.LDHelper.java
private static Map<String, Object> jsonObject(String json) { try {/*ww w .j av a2 s .co m*/ ObjectMapper mapper = new ObjectMapper(); mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); return mapper.readValue(json, new TypeReference<HashMap<String, Object>>() { }); } catch (IOException ex) { System.out.println(ex.toString()); return null; } }