use of org.apache.commons.fileupload.FileItem in project xwiki-platform by xwiki.
the class XWikiAttachmentContent method getContentOutputStream.
/**
* Set the content of the attachment by writing to a provided OutputStream. Content is *not* appended, this method
* clears the content and creates new content. If you want to append content, you can call
* {@link #getContentInputStream()} and copy the content of that into the provided OutputStream. Before closing this
* OutputStream the content will remain the old content prior to the change.
*
* @return an OutputStream into which the caller can set the content of the attachments.
* @since 4.2M3
*/
public OutputStream getContentOutputStream() {
final FileItem fi = getNewFileItem();
final XWikiAttachmentContent xac = this;
final OutputStream fios;
try {
fios = fi.getOutputStream();
} catch (IOException e) {
// so unless it is modified, this should not happen.
throw new RuntimeException("Exception getting attachment OutputStream.", e);
}
return (new ProxyOutputStream(fios) {
@Override
public void close() throws IOException {
super.close();
xac.file = fi;
xac.setContentDirty(true);
if (xac.attachment != null) {
xac.attachment.setLongSize(xac.getLongSize());
}
}
});
}
use of org.apache.commons.fileupload.FileItem in project activityinfo by bedatadriven.
the class AttachmentServlet method doPost.
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String key = request.getParameter("blobId");
Integer siteId = Integer.valueOf(request.getParameter("siteId"));
FileItem fileItem = getFirstUploadFile(request);
String fileName = fileItem.getName();
InputStream uploadingStream = fileItem.getInputStream();
service.upload(key, fileItem, uploadingStream);
CreateSiteAttachment siteAttachment = new CreateSiteAttachment();
siteAttachment.setSiteId(siteId);
siteAttachment.setBlobId(key);
siteAttachment.setFileName(fileName);
siteAttachment.setBlobSize(fileItem.getSize());
siteAttachment.setContentType(fileItem.getContentType());
dispatcher.execute(siteAttachment);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error handling upload", e);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
use of org.apache.commons.fileupload.FileItem in project wicket by apache.
the class FileUploadField method getFileUploads.
/**
* @return a list of all uploaded files. The list is empty if no files were selected. It will return more than one files if:
* <ul>
* <li>HTML5 <input type="file" <strong>multiple</strong> /> is used</li>
* <li>the browser supports <em>multiple</em> attribute</li>
* <li>the user has selected more than one files from the <em>Select file</em> dialog</li>
* </ul>
*/
public List<FileUpload> getFileUploads() {
if (fileUploads != null) {
return fileUploads;
}
fileUploads = new ArrayList<>();
// Get request
final Request request = getRequest();
// If we successfully installed a multipart request
if (request instanceof IMultipartWebRequest) {
// Get the item for the path
final List<FileItem> fileItems = ((IMultipartWebRequest) request).getFile(getInputName());
if (fileItems != null) {
for (FileItem item : fileItems) {
// WICKET-6270 detect empty field by missing file name
if (Strings.isEmpty(item.getName()) == false) {
fileUploads.add(new FileUpload(item));
}
}
}
}
return fileUploads;
}
use of org.apache.commons.fileupload.FileItem in project wicket by apache.
the class MultiFileUploadField method getInputAsArray.
/**
* @see org.apache.wicket.markup.html.form.FormComponent#getInputAsArray()
*/
@Override
public String[] getInputAsArray() {
if (inputArrayCache == null) {
// this array will aggregate all input names
ArrayList<String> names = null;
final Request request = getRequest();
if (request instanceof IMultipartWebRequest) {
// retrieve the filename->FileItem map from request
final Map<String, List<FileItem>> itemNameToItem = ((IMultipartWebRequest) request).getFiles();
for (Entry<String, List<FileItem>> entry : itemNameToItem.entrySet()) {
// iterate over the map and build the list of filenames
final String name = entry.getKey();
final List<FileItem> fileItems = entry.getValue();
if (!Strings.isEmpty(name) && name.startsWith(getInputName() + MAGIC_SEPARATOR) && !fileItems.isEmpty() && !Strings.isEmpty(fileItems.get(0).getName())) {
// make sure the fileitem belongs to this component and
// is not empty
names = (names != null) ? names : new ArrayList<String>();
names.add(name);
}
}
}
if (names != null) {
inputArrayCache = names.toArray(new String[names.size()]);
}
}
return inputArrayCache;
}
use of org.apache.commons.fileupload.FileItem in project wicket by apache.
the class FileUploadTest method getInputStream.
/**
* Test that when getting an input stream a new input stream is returned every time.
*
* Also test that the inputstream is saved internally for later closing.
*
* @throws Exception
*/
@Test
public void getInputStream() throws Exception {
final IFileCleaner fileUploadCleaner = new FileCleaner();
DiskFileItemFactory itemFactory = new DiskFileItemFactory() {
@Override
public FileCleaningTracker getFileCleaningTracker() {
return new FileCleanerTrackerAdapter(fileUploadCleaner);
}
};
FileItem fileItem = itemFactory.createItem("dummyFieldName", "text/java", false, "FileUploadTest.java");
// Initialize the upload
fileItem.getOutputStream();
// Get the internal list out
Field inputStreamsField = FileUpload.class.getDeclaredField("inputStreamsToClose");
inputStreamsField.setAccessible(true);
FileUpload fileUpload = new FileUpload(fileItem);
List<?> inputStreams = (List<?>) inputStreamsField.get(fileUpload);
assertNull(inputStreams);
InputStream is1 = fileUpload.getInputStream();
inputStreams = (List<?>) inputStreamsField.get(fileUpload);
assertEquals(1, inputStreams.size());
InputStream is2 = fileUpload.getInputStream();
inputStreams = (List<?>) inputStreamsField.get(fileUpload);
assertEquals(2, inputStreams.size());
assertNotSame(is1, is2);
// Ok lets close all the streams
try {
fileUpload.closeStreams();
} catch (Exception e) {
fail();
}
inputStreams = (List<?>) inputStreamsField.get(fileUpload);
assertNull(inputStreams);
fileUploadCleaner.destroy();
}
Aggregations