@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { String filePickerResult = ""; if (data != null && resultCode == RESULT_OK) { try { ContentResolver cr = getContentResolver(); Uri uri = data.getData(); Cursor cursor = GeckoApp.mAppContext .getContentResolver() .query(uri, new String[] {OpenableColumns.DISPLAY_NAME}, null, null, null); String name = null; if (cursor != null) { try { if (cursor.moveToNext()) { name = cursor.getString(0); } } finally { cursor.close(); } } String fileName = "tmp_"; String fileExt = null; int period; if (name == null || (period = name.lastIndexOf('.')) == -1) { String mimeType = cr.getType(uri); fileExt = "." + GeckoAppShell.getExtensionFromMimeType(mimeType); } else { fileExt = name.substring(period); fileName = name.substring(0, period); } File file = File.createTempFile(fileName, fileExt, sGREDir); FileOutputStream fos = new FileOutputStream(file); InputStream is = cr.openInputStream(uri); byte[] buf = new byte[4096]; int len = is.read(buf); while (len != -1) { fos.write(buf, 0, len); len = is.read(buf); } fos.close(); filePickerResult = file.getAbsolutePath(); } catch (Exception e) { Log.e(LOG_FILE_NAME, "showing file picker", e); } } try { mFilePickerResult.put(filePickerResult); } catch (InterruptedException e) { Log.i(LOG_FILE_NAME, "error returning file picker result", e); } }
private boolean saveAndVerify(ZipInputStream data) throws IOException { try { ZipEntry ze; while ((ze = data.getNextEntry()) != null) { // Filename + reference to file. String filename = ze.getName(); File output = new File(local_path + filename); if (filename.endsWith("/")) { output.mkdirs(); } else { if (output.exists()) { // Delete the file if it already exists. if (!output.delete()) { return false; } } if (output.createNewFile()) { FileOutputStream out = new FileOutputStream(output); byte[] buffer = new byte[1024]; int count; while ((count = data.read(buffer)) != -1) { out.write(buffer, 0, count); } } else { return false; } } } } catch (Exception e) { e.printStackTrace(); return false; } finally { data.close(); } return true; }
/** * This method handles PUT requests from the client. PUT requests will come from the applet * portion of this application and are the way that images and other files can be posted to the * server. * * @param request the HTTP request object * @param response the HTTP response object * @exception ServletException * @exception IOException */ public void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); /* * The Scan applet will zip all the files together to create a * faster upload and to use just one server connection. */ ZipInputStream in = new ZipInputStream(request.getInputStream()); /* * This will write all the files to a directory on the server. */ try { try { File file = new File("scan"); Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(file)); document.open(); String fileSize = null; while (true) { ZipEntry entry = in.getNextEntry(); if (entry == null) { break; } File f = File.createTempFile("translate", entry.getName()); FileOutputStream out = new FileOutputStream(f); FileInputStream inStream = null; try { int read; byte[] buf = new byte[2024]; while ((read = in.read(buf)) > 0) { out.write(buf, 0, read); } out.close(); inStream = new FileInputStream(f); System.out.println(entry.getSize()); byte[] b = new byte[inStream.available()]; inStream.read(b); System.out.println(b.length); com.itextpdf.text.Image image1 = com.itextpdf.text.Image.getInstance(b); image1.scalePercent(30); image1.setCompressionLevel(9); document.add(image1); } finally { } } document.close(); // fileSize = CommonUtils.getFileSize(file); // DocumentStoreLogDAO documentStoreLogDAO = new DocumentStoreLogDAO(); // DocumentStoreLog documentStoreLog = null; // DocumentStoreLog documentStore = // (DocumentStoreLog)session.getAttribute(CommonConstants.DOCUMENT_STORE_LOG); // if(null != documentStore) { // documentStore.setFileSize(fileSize); // } // Transaction tx = documentStoreLogDAO.getSession().getTransaction(); // tx.begin(); // documentStoreLogDAO.save(documentStore); // tx.commit(); } catch (ZipException ze) { /* * We want to catch each sip exception separately because * there is a possibility that we can read more files from * the archive even if one of them is corrupted. */ ze.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } finally { in.close(); } /* * Now that we have finished uploading the files * we will send a reponse to the server indicating * our success. */ response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); out.println("<html><head><title>ImageSrv</title></head></html>"); out.flush(); out.close(); response.setStatus(HttpServletResponse.SC_OK); }
/** The run method. */ public void run() { instances.add(this); try { listener.startUnpack(); String currentOs = System.getProperty("os.name").toLowerCase(); // // Initialisations FileOutputStream out = null; ArrayList parsables = new ArrayList(); ArrayList executables = new ArrayList(); List packs = idata.selectedPacks; int npacks = packs.size(); udata = UninstallData.getInstance(); // Specific to the web installers if (idata.kind.equalsIgnoreCase("web") || idata.kind.equalsIgnoreCase("web-kunststoff")) { InputStream kin = getClass().getResourceAsStream("/res/WebInstallers.url"); BufferedReader kreader = new BufferedReader(new InputStreamReader(kin)); jarLocation = kreader.readLine(); } // We unpack the selected packs for (int i = 0; i < npacks; i++) { // We get the pack stream int n = idata.allPacks.indexOf(packs.get(i)); ObjectInputStream objIn = new ObjectInputStream(getPackAsStream(n)); // We unpack the files int nfiles = objIn.readInt(); listener.changeUnpack(0, nfiles, ((Pack) packs.get(i)).name); for (int j = 0; j < nfiles; j++) { // We read the header PackFile pf = (PackFile) objIn.readObject(); if (null == pf.os || matchOS(currentOs, pf.os.toLowerCase())) { // We translate & build the path String path = translatePath(pf.targetPath); File pathFile = new File(path); String fname = pathFile.getName(); int z = fname.length(); File dest = pathFile.getParentFile(); if (!dest.exists()) dest.mkdirs(); // We add the path to the log, udata.addFile(path); listener.progressUnpack(j, path); // if this file exists and shouldnot override skip this file if (((pf.override == false) && (pathFile.exists()))) { objIn.skip(pf.length); continue; } // We copy the file out = new FileOutputStream(path); byte[] buffer = new byte[5120]; long bytesCopied = 0; while (bytesCopied < pf.length) { int maxBytes = (pf.length - bytesCopied < buffer.length ? (int) (pf.length - bytesCopied) : buffer.length); int bytesInBuffer = objIn.read(buffer, 0, maxBytes); if (bytesInBuffer == -1) throw new IOException("Unexpected end of stream"); out.write(buffer, 0, bytesInBuffer); bytesCopied += bytesInBuffer; } // Cleanings out.close(); // Empty dirs restoring String _n = pathFile.getName(); if (_n.startsWith("izpack-keepme") && _n.endsWith(".tmp")) pathFile.delete(); } else objIn.skip(pf.length); } // Load information about parsable files int numParsables = objIn.readInt(); int k; for (k = 0; k < numParsables; k++) { ParsableFile pf = (ParsableFile) objIn.readObject(); pf.path = translatePath(pf.path); parsables.add(pf); } // Load information about executable files int numExecutables = objIn.readInt(); for (k = 0; k < numExecutables; k++) { ExecutableFile ef = (ExecutableFile) objIn.readObject(); ef.path = translatePath(ef.path); if (null != ef.argList && !ef.argList.isEmpty()) { String arg = null; for (int j = 0; j < ef.argList.size(); j++) { arg = (String) ef.argList.get(j); arg = translatePath(arg); ef.argList.set(j, arg); } } executables.add(ef); if (ef.executionStage == ExecutableFile.UNINSTALL) { udata.addExecutable(ef); } } objIn.close(); } // We use the scripts parser ScriptParser parser = new ScriptParser(parsables, vs); parser.parseFiles(); // We use the file executor FileExecutor executor = new FileExecutor(executables); if (executor.executeFiles(ExecutableFile.POSTINSTALL) != 0) javax.swing.JOptionPane.showMessageDialog( null, "The installation was not completed.", "Installation warning", javax.swing.JOptionPane.WARNING_MESSAGE); // We put the uninstaller putUninstaller(); // The end :-) listener.stopUnpack(); } catch (Exception err) { listener.stopUnpack(); listener.errorUnpack(err.toString()); } instances.remove(instances.indexOf(this)); }
public static void main(String[] args) throws IOException { int servPort = Integer.parseInt(args[0]); String ksName = "keystore.jks"; char ksPass[] = "password".toCharArray(); char ctPass[] = "password".toCharArray(); try { KeyStore ks = KeyStore.getInstance("JKS"); ks.load(new FileInputStream(ksName), ksPass); KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); kmf.init(ks, ctPass); SSLContext sc = SSLContext.getInstance("SSL"); sc.init(kmf.getKeyManagers(), null, null); SSLServerSocketFactory ssf = sc.getServerSocketFactory(); SSLServerSocket s = (SSLServerSocket) ssf.createServerSocket(servPort); while (true) { // Listen for a TCP connection request. SSLSocket c = (SSLSocket) s.accept(); BufferedOutputStream out = new BufferedOutputStream(c.getOutputStream(), 1024); BufferedInputStream in = new BufferedInputStream(c.getInputStream(), 1024); byte[] byteBuffer = new byte[1024]; int count = 0; while ((byteBuffer[count] = (byte) in.read()) != -2) { count++; } String newFile = new String(byteBuffer, 0, count, "US-ASCII"); FileOutputStream writer = new FileOutputStream(newFile.trim()); int buffSize = 0; while ((buffSize = in.read(byteBuffer, 0, 1024)) != -1) { int index = 0; if ((index = (new String(byteBuffer, 0, buffSize, "US-ASCII")) .indexOf("------MagicStringCSE283Miami")) == -1) { writer.write(byteBuffer, 0, buffSize); } else { writer.write(byteBuffer, 0, index); break; } } writer.flush(); writer.close(); ZipOutputStream outZip = new ZipOutputStream(new BufferedOutputStream(out)); FileInputStream fin = new FileInputStream(newFile.trim()); BufferedInputStream origin = new BufferedInputStream(fin, 1024); ZipEntry entry = new ZipEntry(newFile.trim()); outZip.putNextEntry(entry); byteBuffer = new byte[1024]; int bytes = 0; while ((bytes = origin.read(byteBuffer, 0, 1024)) != -1) { outZip.write(byteBuffer, 0, bytes); } origin.close(); outZip.flush(); outZip.close(); out.flush(); out.close(); } } catch (Exception e) { System.err.println(e.toString()); } }