List of usage examples for org.apache.commons.fileupload FileItem getOutputStream
OutputStream getOutputStream() throws IOException;
From source file:de.metalcon.musicStorageServer.protocol.CreateRequestTest.java
/** * create a music item// w w w.ja va2 s. c o m * * @param contentType * content type of the music file * @param musicFile * file handle to the music file * @return music item representing the music file passed */ private static FileItem createMusicItem(final String contentType, final File musicFile) { final FileItem musicItem = new DiskFileItem(ProtocolConstants.Parameter.Create.MUSIC_ITEM, contentType, false, musicFile.getName(), (int) musicFile.length(), DISK_FILE_REPOSITORY); // reason for call of getOutputStream: bug in commons.IO // called anyway to create file try { final OutputStream outputStream = musicItem.getOutputStream(); final InputStream inputStream = new FileInputStream(musicFile); IOUtils.copy(inputStream, outputStream); } catch (final IOException e) { e.printStackTrace(); fail("music item creation failed!"); } return musicItem; }
From source file:de.metalcon.imageServer.protocol.CreateRequestTest.java
/** * create an image item//from ww w .j a v a 2s .co m * * @param contentType * content type of the music file * @param musicFile * file handle to the music file * @return music item representing the music file passed */ private static FileItem createImageItem(final String contentType, final File imageFile) { final FileItem imageItem = new DiskFileItem(ProtocolConstants.Parameters.Create.IMAGE_ITEM, contentType, false, imageFile.getName(), (int) imageFile.length(), DISK_FILE_REPOSITORY); // reason for call of getOutputStream: bug in commons.IO // called anyway to create file try { final OutputStream outputStream = imageItem.getOutputStream(); final InputStream inputStream = new FileInputStream(imageFile); IOUtils.copy(inputStream, outputStream); } catch (final IOException e) { e.printStackTrace(); fail("image item creation failed!"); } return imageItem; }
From source file:com.linwoodhomes.TempFileTest.java
@Test public void testGetTempFile() { FileItem fileItem = tf.getTempFile(); try {/* w ww. java2 s .c o m*/ Assert.assertNotNull(fileItem.getOutputStream()); } catch (IOException e) { Assert.fail("Couldn't open output stream"); } }
From source file:com.enonic.vertical.adminweb.handlers.fieldtypes.Field.java
public FileItem createFileItem(String fileName, byte[] data) { FileItem binary = null; try {//from w w w . j a va2s .c o m binary = fileUpload.getFileItemFactory().createItem(name, null, false, fileName); binary.getOutputStream().write(data); } catch (IOException ioe) { throw new RuntimeException(ioe); } return binary; }
From source file:echopoint.tucana.JakartaCommonsFileUploadProvider.java
/** * @see UploadSPI#handleUpload(nextapp.echo.webcontainer.Connection , * FileUploadSelector, String, UploadProgress) *///from w ww . j ava 2s .c om @SuppressWarnings({ "ThrowableInstanceNeverThrown" }) public void handleUpload(final Connection conn, final FileUploadSelector uploadSelect, final String uploadIndex, final UploadProgress progress) throws Exception { final ApplicationInstance app = conn.getUserInstance().getApplicationInstance(); final DiskFileItemFactory itemFactory = new DiskFileItemFactory(); itemFactory.setRepository(getDiskCacheLocation()); itemFactory.setSizeThreshold(getMemoryCacheThreshold()); String encoding = conn.getRequest().getCharacterEncoding(); if (encoding == null) { encoding = "UTF-8"; } final ServletFileUpload upload = new ServletFileUpload(itemFactory); upload.setHeaderEncoding(encoding); upload.setProgressListener(new UploadProgressListener(progress)); long sizeLimit = uploadSelect.getUploadSizeLimit(); if (sizeLimit == 0) sizeLimit = getFileUploadSizeLimit(); if (sizeLimit != NO_SIZE_LIMIT) { upload.setSizeMax(sizeLimit); } String fileName = null; String contentType = null; try { final FileItemIterator iter = upload.getItemIterator(conn.getRequest()); if (iter.hasNext()) { final FileItemStream stream = iter.next(); if (!stream.isFormField()) { fileName = FilenameUtils.getName(stream.getName()); contentType = stream.getContentType(); final Set<String> types = uploadSelect.getContentTypeFilter(); if (!types.isEmpty()) { if (!types.contains(contentType)) { app.enqueueTask(uploadSelect.getTaskQueue(), new InvalidContentTypeRunnable( uploadSelect, uploadIndex, fileName, contentType, progress)); return; } } progress.setStatus(Status.inprogress); final FileItem item = itemFactory.createItem(fileName, contentType, false, stream.getName()); item.getOutputStream(); // initialise DiskFileItem internals uploadSelect.notifyCallback( new UploadStartEvent(uploadSelect, uploadIndex, fileName, contentType, item.getSize())); IOUtils.copy(stream.openStream(), item.getOutputStream()); app.enqueueTask(uploadSelect.getTaskQueue(), new FinishRunnable(uploadSelect, uploadIndex, fileName, item, progress)); return; } } app.enqueueTask(uploadSelect.getTaskQueue(), new FailRunnable(uploadSelect, uploadIndex, fileName, contentType, new RuntimeException("No multi-part content!"), progress)); } catch (final FileUploadBase.SizeLimitExceededException e) { app.enqueueTask(uploadSelect.getTaskQueue(), new FailRunnable(uploadSelect, uploadIndex, fileName, contentType, new UploadSizeLimitExceededException(e), progress)); } catch (final FileUploadBase.FileSizeLimitExceededException e) { app.enqueueTask(uploadSelect.getTaskQueue(), new FailRunnable(uploadSelect, uploadIndex, fileName, contentType, new UploadSizeLimitExceededException(e), progress)); } catch (final MultipartStream.MalformedStreamException e) { app.enqueueTask(uploadSelect.getTaskQueue(), new CancelRunnable(uploadSelect, uploadIndex, fileName, contentType, e, progress)); } }
From source file:com.carolinarollergirls.scoreboard.jetty.MediaServlet.java
protected void processZipFileItem(FileItemFactory factory, FileItem zip, List<FileItem> fileItems) throws IOException { ZipInputStream ziS = new ZipInputStream(zip.getInputStream()); ZipEntry zE;//from ww w. j a va2 s.co m try { while (null != (zE = ziS.getNextEntry())) { if (zE.isDirectory() || !uploadFileNameFilter.accept(null, zE.getName())) continue; FileItem item = factory.createItem(null, null, false, zE.getName()); OutputStream oS = item.getOutputStream(); IOUtils.copyLarge(ziS, oS); oS.close(); fileItems.add(item); } } finally { ziS.close(); } }
From source file:com.zimbra.cs.service.FileUploadServlet.java
public static Upload saveUpload(InputStream is, String filename, String contentType, String accountId, long limit) throws ServiceException, IOException { FileItem fi = null; boolean success = false; try {/*from w w w . j a va 2s. c o m*/ // store the fetched file as a normal upload ServletFileUpload upload = getUploader(limit); long sizeMax = upload.getSizeMax(); fi = upload.getFileItemFactory().createItem("upload", contentType, false, filename); // sizeMax=-1 means "no limit" long size = ByteUtil.copy(is, true, fi.getOutputStream(), true, sizeMax < 0 ? sizeMax : sizeMax + 1); if (upload.getSizeMax() >= 0 && size > upload.getSizeMax()) { mLog.info("Exceeded maximum upload size of " + upload.getSizeMax() + " bytes"); throw MailServiceException.UPLOAD_TOO_LARGE(filename, "upload too large"); } Upload up = new Upload(accountId, fi); mLog.info("saveUpload(): received %s", up); synchronized (mPending) { mPending.put(up.uuid, up); } success = true; return up; } finally { if (!success && fi != null) { mLog.debug("saveUpload(): unsuccessful attempt. Deleting %s", fi); fi.delete(); } } }
From source file:hoot.services.ingest.MultipartSerializerTest.java
@Test @Category(UnitTest.class) public void TestserializeUploadedFiles() throws Exception { //homeFolder + "/upload/" + jobId + "/" + relPath; // Create dummy FGDB String jobId = "123-456-789-testosm"; String wkdirpath = homeFolder + "/upload/" + jobId; File workingDir = new File(wkdirpath); FileUtils.forceMkdir(workingDir);/*from w ww. j av a2s . com*/ org.junit.Assert.assertTrue(workingDir.exists()); List<FileItem> fileItemsList = new ArrayList<FileItem>(); FileItemFactory factory = new DiskFileItemFactory(16, null); String textFieldName = "textField"; FileItem item = factory.createItem(textFieldName, "application/octet-stream", true, "dummy1.osm"); String textFieldValue = "0123456789"; byte[] testFieldValueBytes = textFieldValue.getBytes(); OutputStream os = item.getOutputStream(); os.write(testFieldValueBytes); os.close(); File out = new File(wkdirpath + "/buffer.tmp"); item.write(out); fileItemsList.add(item); org.junit.Assert.assertTrue(out.exists()); Map<String, String> uploadedFiles = new HashMap<String, String>(); Map<String, String> uploadedFilesPaths = new HashMap<String, String>(); _ms._serializeUploadedFiles(fileItemsList, jobId, uploadedFiles, uploadedFilesPaths, wkdirpath); org.junit.Assert.assertEquals("OSM", uploadedFiles.get("dummy1")); org.junit.Assert.assertEquals("dummy1.osm", uploadedFilesPaths.get("dummy1")); File content = new File(wkdirpath + "/dummy1.osm"); org.junit.Assert.assertTrue(content.exists()); FileUtils.forceDelete(workingDir); }
From source file:com.github.davidcarboni.encryptedfileupload.EncryptedFileItemSerializeTest.java
/** * Create a FileItem with the specfied content bytes and repository. *//*from www. j a va 2 s . c o m*/ private FileItem createFileItem(byte[] contentBytes, File repository) { FileItemFactory factory = new EncryptedFileItemFactory(threshold, repository); String textFieldName = "textField"; FileItem item = factory.createItem(textFieldName, textContentType, true, "My File Name"); try { OutputStream os = item.getOutputStream(); os.write(contentBytes); os.close(); } catch (IOException e) { fail("Unexpected IOException" + e); } return item; }
From source file:hoot.services.ingest.MultipartSerializerTest.java
@Test @Category(UnitTest.class) public void TestserializeFGDB() throws Exception { String jobId = "123-456-789"; String wkdirpath = homeFolder + "/upload/" + jobId; File workingDir = new File(wkdirpath); FileUtils.forceMkdir(workingDir);//from ww w . ja va2 s . c o m org.junit.Assert.assertTrue(workingDir.exists()); List<FileItem> fileItemsList = new ArrayList<FileItem>(); FileItemFactory factory = new DiskFileItemFactory(16, null); String textFieldName = "textField"; FileItem item = factory.createItem(textFieldName, "application/octet-stream", true, "fgdbTest.gdb/dummy1.gdbtable"); String textFieldValue = "0123456789"; byte[] testFieldValueBytes = textFieldValue.getBytes(); OutputStream os = item.getOutputStream(); os.write(testFieldValueBytes); os.close(); File out = new File(wkdirpath + "/buffer.tmp"); item.write(out); fileItemsList.add(item); org.junit.Assert.assertTrue(out.exists()); Map<String, String> uploadedFiles = new HashMap<String, String>(); Map<String, String> uploadedFilesPaths = new HashMap<String, String>(); _ms._serializeFGDB(fileItemsList, jobId, uploadedFiles, uploadedFilesPaths); org.junit.Assert.assertEquals("GDB", uploadedFiles.get("fgdbTest")); org.junit.Assert.assertEquals("fgdbTest.gdb", uploadedFilesPaths.get("fgdbTest")); File fgdbpath = new File(wkdirpath + "/fgdbTest.gdb"); org.junit.Assert.assertTrue(fgdbpath.exists()); File content = new File(wkdirpath + "/fgdbTest.gdb/dummy1.gdbtable"); org.junit.Assert.assertTrue(content.exists()); FileUtils.forceDelete(workingDir); }