From a1e307047f5b91af3045c3f26c72df9906646090 Mon Sep 17 00:00:00 2001 From: karppa Date: Thu, 9 Aug 2012 22:42:09 +0300 Subject: [PATCH] Somewhat an excercise to support v5-branch of jQuery-File-Upload --- WebContent/WEB-INF/web.xml | 47 +- .../_del_fileupload/jquery.fileupload-ui.css | 74 --- .../_del_fileupload/jquery.fileupload-ui.js | 308 --------- .../_del_fileupload/jquery.fileupload.js | 629 ------------------ WebContent/_del_fileupload/pbar-ani.gif | Bin 304064 -> 0 bytes WebContent/index.html | 2 +- nbproject/build-impl.xml | 4 + nbproject/genfiles.properties | 6 +- nbproject/project.properties | 4 +- nbproject/project.xml | 8 + src/info/sudr/file/UploadServlet.java | 210 ++++-- 11 files changed, 205 insertions(+), 1087 deletions(-) delete mode 100644 WebContent/_del_fileupload/jquery.fileupload-ui.css delete mode 100644 WebContent/_del_fileupload/jquery.fileupload-ui.js delete mode 100644 WebContent/_del_fileupload/jquery.fileupload.js delete mode 100644 WebContent/_del_fileupload/pbar-ani.gif diff --git a/WebContent/WEB-INF/web.xml b/WebContent/WEB-INF/web.xml index d3214c9..63bc949 100644 --- a/WebContent/WEB-INF/web.xml +++ b/WebContent/WEB-INF/web.xml @@ -1,22 +1,27 @@ - - fileupload - - index.html - index.htm - index.jsp - default.html - default.htm - default.jsp - - - - UploadServlet - UploadServlet - info.sudr.file.UploadServlet - - - UploadServlet - /upload - - \ No newline at end of file + + fileupload + + index.html + index.htm + index.jsp + default.html + default.htm + default.jsp + + + + UploadServlet + UploadServlet + info.sudr.file.UploadServlet + + Define storagepath for fileuploads + upload_path + /media/data/fileUpload + + + + UploadServlet + /upload + + diff --git a/WebContent/_del_fileupload/jquery.fileupload-ui.css b/WebContent/_del_fileupload/jquery.fileupload-ui.css deleted file mode 100644 index 43e576a..0000000 --- a/WebContent/_del_fileupload/jquery.fileupload-ui.css +++ /dev/null @@ -1,74 +0,0 @@ -div.file_upload { - position: relative; - overflow: hidden; - direction: ltr; - cursor: pointer; - text-align: center; - color: #333; - font-weight: bold; - -moz-border-radius: 10px; - -webkit-border-radius: 10px; - border-radius: 10px; - width: 200px; - height: 30px; - line-height: 30px; - background: palegreen; - border: 1px solid limegreen; -} - -div.file_upload_small { - width: 200px; - height: 30px; - line-height: 30px; - font-size: auto; - background: palegreen; - border: 1px solid limegreen; -} - -div.file_upload_large { - width: 100%; - height: 150px; - line-height: 150px; - font-size: 20px; - background: palegreen; - border: 1px solid limegreen; -} - -div.file_upload_highlight { - background: lawngreen; -} - -div.file_upload input { - position: absolute; - top: 0; - right: 0; - margin: 0; - border: 300px solid transparent; - opacity: 0; - -ms-filter: 'alpha(opacity=0)'; - filter: alpha(opacity=0); - -o-transform: translate(-300px, -300px) scale(10); - -moz-transform: translate(-800px, 0) scale(10); - cursor: pointer; -} - -.file_upload iframe, .file_upload button { - display: none; -} - -.file_upload_preview img { - width: 80px; -} - -.file_upload_progress .ui-progressbar-value { - background: url(pbar-ani.gif); -} - -.file_upload_progress div { - width: 150px; - height: 15px; -} - -.file_upload_cancel button { - cursor: pointer; -} \ No newline at end of file diff --git a/WebContent/_del_fileupload/jquery.fileupload-ui.js b/WebContent/_del_fileupload/jquery.fileupload-ui.js deleted file mode 100644 index 4223be0..0000000 --- a/WebContent/_del_fileupload/jquery.fileupload-ui.js +++ /dev/null @@ -1,308 +0,0 @@ -/* - * jQuery File Upload User Interface Plugin 3.6 - * - * Copyright 2010, Sebastian Tschan, AQUANTUM - * Licensed under the MIT license: - * http://creativecommons.org/licenses/MIT/ - * - * https://blueimp.net - * http://www.aquantum.de - */ - -/*jslint browser: true */ -/*global jQuery, FileReader, URL */ - -(function ($) { - - var undef = 'undefined', - func = 'function', - UploadHandler, - methods, - - LocalImage = function (file, imageTypes) { - var img, - fileReader; - if (!imageTypes.test(file.type)) { - return null; - } - img = document.createElement('img'); - if (typeof URL !== undef && typeof URL.createObjectURL === func) { - img.src = URL.createObjectURL(file); - img.onload = function () { - URL.revokeObjectURL(this.src); - }; - return img; - } - if (typeof FileReader !== undef) { - fileReader = new FileReader(); - if (typeof fileReader.readAsDataURL === func) { - fileReader.onload = function (e) { - img.src = e.target.result; - }; - fileReader.readAsDataURL(file); - return img; - } - } - return null; - }; - - UploadHandler = function (container, options) { - var uploadHandler = this, - dragOverTimeout, - isDropZoneEnlarged; - - this.dropZone = container; - this.imageTypes = /^image\/(gif|jpeg|png)$/; - this.previewSelector = '.file_upload_preview'; - this.progressSelector = '.file_upload_progress div'; - this.cancelSelector = '.file_upload_cancel button'; - this.cssClassSmall = 'file_upload_small'; - this.cssClassLarge = 'file_upload_large'; - this.cssClassHighlight = 'file_upload_highlight'; - this.dropEffect = 'highlight'; - this.uploadTable = this.downloadTable = null; - - this.buildUploadRow = this.buildDownloadRow = function () { - return null; - }; - - this.addNode = function (parentNode, node, callBack) { - if (node) { - node.css('display', 'none').appendTo(parentNode).fadeIn(function () { - if (typeof callBack === func) { - try { - callBack(); - } catch (e) { - // Fix endless exception loop: - $(this).stop(); - throw e; - } - } - }); - } else if (typeof callBack === func) { - callBack(); - } - }; - - this.removeNode = function (node, callBack) { - if (node) { - node.fadeOut(function () { - $(this).remove(); - if (typeof callBack === func) { - try { - callBack(); - } catch (e) { - // Fix endless exception loop: - $(this).stop(); - throw e; - } - } - }); - } else if (typeof callBack === func) { - callBack(); - } - }; - - this.onAbort = function (event, files, index, xhr, handler) { - handler.removeNode(handler.uploadRow); - }; - - this.cancelUpload = function (event, files, index, xhr, handler) { - var readyState = xhr.readyState; - xhr.abort(); - // If readyState is below 2, abort() has no effect: - if (isNaN(readyState) || readyState < 2) { - handler.onAbort(event, files, index, xhr, handler); - } - }; - - this.initProgressBar = function (node, value) { - if (typeof node.progressbar === func) { - return node.progressbar({ - value: value - }); - } else { - var progressbar = $('').appendTo(node); - progressbar.progressbar = function (key, value) { - progressbar.attr('value', value); - }; - return progressbar; - } - }; - - this.initUploadRow = function (event, files, index, xhr, handler, callBack) { - var uploadRow = handler.uploadRow = handler.buildUploadRow(files, index, handler); - if (uploadRow) { - handler.progressbar = handler.initProgressBar( - uploadRow.find(handler.progressSelector), - (xhr.upload ? 0 : 100) - ); - uploadRow.find(handler.cancelSelector).click(function (e) { - handler.cancelUpload(e, files, index, xhr, handler); - }); - uploadRow.find(handler.previewSelector).each(function () { - $(this).append(new LocalImage(files[index], handler.imageTypes)); - }); - } - handler.addNode( - (typeof handler.uploadTable === func ? handler.uploadTable(handler) : handler.uploadTable), - uploadRow, - callBack - ); - }; - - this.initUpload = function (event, files, index, xhr, handler, callBack) { - handler.initUploadRow(event, files, index, xhr, handler, function () { - if (typeof handler.beforeSend === func) { - handler.beforeSend(event, files, index, xhr, handler, callBack); - } else { - callBack(); - } - }); - }; - - this.onProgress = function (event, files, index, xhr, handler) { - if (handler.progressbar) { - handler.progressbar.progressbar( - 'value', - parseInt(event.loaded / event.total * 100, 10) - ); - } - }; - - this.parseResponse = function (xhr) { - if (typeof xhr.responseText !== undef) { - return $.parseJSON(xhr.responseText); - } else { - // Instead of an XHR object, an iframe is used for legacy browsers: - return $.parseJSON(xhr.contents().text()); - } - }; - - this.initDownloadRow = function (event, files, index, xhr, handler, callBack) { - var json, downloadRow; - try { - json = handler.response = handler.parseResponse(xhr); - downloadRow = handler.downloadRow = handler.buildDownloadRow(json, handler); - handler.addNode( - (typeof handler.downloadTable === func ? handler.downloadTable(handler) : handler.downloadTable), - downloadRow, - callBack - ); - } catch (e) { - if (typeof handler.onError === func) { - handler.originalEvent = event; - handler.onError(e, files, index, xhr, handler); - } else { - throw e; - } - } - }; - - this.onLoad = function (event, files, index, xhr, handler) { - handler.removeNode(handler.uploadRow, function () { - handler.initDownloadRow(event, files, index, xhr, handler, function () { - if (typeof handler.onComplete === func) { - handler.onComplete(event, files, index, xhr, handler); - } - }); - }); - }; - - this.dropZoneEnlarge = function () { - if (!isDropZoneEnlarged) { - if (typeof uploadHandler.dropZone.switchClass === func) { - uploadHandler.dropZone.switchClass( - uploadHandler.cssClassSmall, - uploadHandler.cssClassLarge - ); - } else { - uploadHandler.dropZone.addClass(uploadHandler.cssClassLarge); - uploadHandler.dropZone.removeClass(uploadHandler.cssClassSmall); - } - isDropZoneEnlarged = true; - } - }; - - this.dropZoneReduce = function () { - if (typeof uploadHandler.dropZone.switchClass === func) { - uploadHandler.dropZone.switchClass( - uploadHandler.cssClassLarge, - uploadHandler.cssClassSmall - ); - } else { - uploadHandler.dropZone.addClass(uploadHandler.cssClassSmall); - uploadHandler.dropZone.removeClass(uploadHandler.cssClassLarge); - } - isDropZoneEnlarged = false; - }; - - this.onDocumentDragEnter = function (event) { - uploadHandler.dropZoneEnlarge(); - }; - - this.onDocumentDragOver = function (event) { - if (dragOverTimeout) { - clearTimeout(dragOverTimeout); - } - dragOverTimeout = setTimeout(function () { - uploadHandler.dropZoneReduce(); - }, 200); - }; - - this.onDragEnter = this.onDragLeave = function (event) { - uploadHandler.dropZone.toggleClass(uploadHandler.cssClassHighlight); - }; - - this.onDrop = function (event) { - if (dragOverTimeout) { - clearTimeout(dragOverTimeout); - } - if (uploadHandler.dropEffect && typeof uploadHandler.dropZone.effect === func) { - uploadHandler.dropZone.effect(uploadHandler.dropEffect, function () { - uploadHandler.dropZone.removeClass(uploadHandler.cssClassHighlight); - uploadHandler.dropZoneReduce(); - }); - } else { - uploadHandler.dropZone.removeClass(uploadHandler.cssClassHighlight); - uploadHandler.dropZoneReduce(); - } - }; - - $.extend(this, options); - }; - - methods = { - init : function (options) { - return this.each(function () { - $(this).fileUpload(new UploadHandler($(this), options)); - }); - }, - - option: function (option, value, namespace) { - if (typeof option === undef || (typeof option === 'string' && typeof value === undef)) { - return $(this).fileUpload('option', option, value, namespace); - } - return this.each(function () { - $(this).fileUpload('option', option, value, namespace); - }); - }, - - destroy : function (namespace) { - return this.each(function () { - $(this).fileUpload('destroy', namespace); - }); - } - }; - - $.fn.fileUploadUI = function (method) { - if (methods[method]) { - return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); - } else if (typeof method === 'object' || !method) { - return methods.init.apply(this, arguments); - } else { - $.error('Method ' + method + ' does not exist on jQuery.fileUploadUI'); - } - }; - -}(jQuery)); \ No newline at end of file diff --git a/WebContent/_del_fileupload/jquery.fileupload.js b/WebContent/_del_fileupload/jquery.fileupload.js deleted file mode 100644 index 6a5c39d..0000000 --- a/WebContent/_del_fileupload/jquery.fileupload.js +++ /dev/null @@ -1,629 +0,0 @@ -/* - * jQuery File Upload Plugin 3.7.1 - * - * Copyright 2010, Sebastian Tschan, AQUANTUM - * Licensed under the MIT license: - * http://creativecommons.org/licenses/MIT/ - * - * https://blueimp.net - * http://www.aquantum.de - */ - -/*jslint browser: true */ -/*global File, FileReader, FormData, unescape, jQuery */ - -(function ($) { - - var defaultNamespace = 'file_upload', - undef = 'undefined', - func = 'function', - num = 'number', - FileUpload, - methods, - - MultiLoader = function (callBack, numberComplete) { - var loaded = 0; - this.complete = function () { - loaded += 1; - if (loaded === numberComplete) { - callBack(); - } - }; - }; - - FileUpload = function (container) { - var fileUpload = this, - uploadForm, - fileInput, - settings = { - namespace: defaultNamespace, - uploadFormFilter: function (index) { - return true; - }, - fileInputFilter: function (index) { - return true; - }, - cssClass: defaultNamespace, - dragDropSupport: true, - dropZone: container, - url: function (form) { - return form.attr('action'); - }, - method: function (form) { - return form.attr('method'); - }, - fieldName: function (input) { - return input.attr('name'); - }, - formData: function (form) { - return form.serializeArray(); - }, - multipart: true, - multiFileRequest: false, - withCredentials: false, - forceIframeUpload: false - }, - documentListeners = {}, - dropZoneListeners = {}, - protocolRegExp = /^http(s)?:\/\//, - optionsReference, - - isXHRUploadCapable = function () { - return typeof XMLHttpRequest !== undef && typeof File !== undef && ( - !settings.multipart || typeof FormData !== undef || typeof FileReader !== undef - ); - }, - - initEventHandlers = function () { - if (settings.dragDropSupport) { - if (typeof settings.onDocumentDragEnter === func) { - documentListeners['dragenter.' + settings.namespace] = function (e) { - settings.onDocumentDragEnter(e); - }; - } - if (typeof settings.onDocumentDragLeave === func) { - documentListeners['dragleave.' + settings.namespace] = function (e) { - settings.onDocumentDragLeave(e); - }; - } - documentListeners['dragover.' + settings.namespace] = fileUpload.onDocumentDragOver; - documentListeners['drop.' + settings.namespace] = fileUpload.onDocumentDrop; - $(document).bind(documentListeners); - if (typeof settings.onDragEnter === func) { - dropZoneListeners['dragenter.' + settings.namespace] = function (e) { - settings.onDragEnter(e); - }; - } - if (typeof settings.onDragLeave === func) { - dropZoneListeners['dragleave.' + settings.namespace] = function (e) { - settings.onDragLeave(e); - }; - } - dropZoneListeners['dragover.' + settings.namespace] = fileUpload.onDragOver; - dropZoneListeners['drop.' + settings.namespace] = fileUpload.onDrop; - settings.dropZone.bind(dropZoneListeners); - } - fileInput.bind('change.' + settings.namespace, fileUpload.onChange); - }, - - removeEventHandlers = function () { - $.each(documentListeners, function (key, value) { - $(document).unbind(key, value); - }); - $.each(dropZoneListeners, function (key, value) { - settings.dropZone.unbind(key, value); - }); - fileInput.unbind('change.' + settings.namespace); - }, - - initUploadEventHandlers = function (files, index, xhr, settings) { - if (typeof settings.onProgress === func) { - xhr.upload.onprogress = function (e) { - settings.onProgress(e, files, index, xhr, settings); - }; - } - if (typeof settings.onLoad === func) { - xhr.onload = function (e) { - settings.onLoad(e, files, index, xhr, settings); - }; - } - if (typeof settings.onAbort === func) { - xhr.onabort = function (e) { - settings.onAbort(e, files, index, xhr, settings); - }; - } - if (typeof settings.onError === func) { - xhr.onerror = function (e) { - settings.onError(e, files, index, xhr, settings); - }; - } - }, - - getUrl = function (settings) { - if (typeof settings.url === func) { - return settings.url(settings.uploadForm || uploadForm); - } - return settings.url; - }, - - getMethod = function (settings) { - if (typeof settings.method === func) { - return settings.method(settings.uploadForm || uploadForm); - } - return settings.method; - }, - - getFieldName = function (settings) { - if (typeof settings.fieldName === func) { - return settings.fieldName(settings.fileInput || fileInput); - } - return settings.fieldName; - }, - - getFormData = function (settings) { - var formData; - if (typeof settings.formData === func) { - return settings.formData(settings.uploadForm || uploadForm); - } else if ($.isArray(settings.formData)) { - return settings.formData; - } else if (settings.formData) { - formData = []; - $.each(settings.formData, function (name, value) { - formData.push({name: name, value: value}); - }); - return formData; - } - return []; - }, - - isSameDomain = function (url) { - if (protocolRegExp.test(url)) { - var host = location.host, - indexStart = location.protocol.length + 2, - index = url.indexOf(host, indexStart), - pathIndex = index + host.length; - if ((index === indexStart || index === url.indexOf('@', indexStart) + 1) && - (url.length === pathIndex || $.inArray(url.charAt(pathIndex), ['/', '?', '#']) !== -1)) { - return true; - } - return false; - } - return true; - }, - - nonMultipartUpload = function (file, xhr, sameDomain) { - if (sameDomain) { - xhr.setRequestHeader('X-File-Name', unescape(encodeURIComponent(file.name))); - } - xhr.setRequestHeader('Content-Type', file.type); - xhr.send(file); - }, - - formDataUpload = function (files, xhr, settings) { - var formData = new FormData(), - i; - $.each(getFormData(settings), function (index, field) { - formData.append(field.name, field.value); - }); - for (i = 0; i < files.length; i += 1) { - formData.append(getFieldName(settings), files[i]); - } - xhr.send(formData); - }, - - loadFileContent = function (file, callBack) { - var fileReader = new FileReader(); - fileReader.onload = function (e) { - file.content = e.target.result; - callBack(); - }; - fileReader.readAsBinaryString(file); - }, - - buildMultiPartFormData = function (boundary, files, filesFieldName, fields) { - var doubleDash = '--', - crlf = '\r\n', - formData = ''; - $.each(fields, function (index, field) { - formData += doubleDash + boundary + crlf + - 'Content-Disposition: form-data; name="' + - unescape(encodeURIComponent(field.name)) + - '"' + crlf + crlf + - unescape(encodeURIComponent(field.value)) + crlf; - }); - $.each(files, function (index, file) { - formData += doubleDash + boundary + crlf + - 'Content-Disposition: form-data; name="' + - unescape(encodeURIComponent(filesFieldName)) + - '"; filename="' + unescape(encodeURIComponent(file.name)) + '"' + crlf + - 'Content-Type: ' + file.type + crlf + crlf + - file.content + crlf; - }); - formData += doubleDash + boundary + doubleDash + crlf; - return formData; - }, - - fileReaderUpload = function (files, xhr, settings) { - var boundary = '----MultiPartFormBoundary' + (new Date()).getTime(), - loader, - i; - xhr.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + boundary); - loader = new MultiLoader(function () { - xhr.sendAsBinary(buildMultiPartFormData( - boundary, - files, - getFieldName(settings), - getFormData(settings) - )); - }, files.length); - for (i = 0; i < files.length; i += 1) { - loadFileContent(files[i], loader.complete); - } - }, - - upload = function (files, index, xhr, settings) { - var url = getUrl(settings), - sameDomain = isSameDomain(url), - filesToUpload; - initUploadEventHandlers(files, index, xhr, settings); - xhr.open(getMethod(settings), url, true); - if (sameDomain) { - xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); - } else if (settings.withCredentials) { - xhr.withCredentials = true; - } - if (!settings.multipart) { - nonMultipartUpload(files[index], xhr, sameDomain); - } else { - if (typeof index === num) { - filesToUpload = [files[index]]; - } else { - filesToUpload = files; - } - if (typeof FormData !== undef) { - formDataUpload(filesToUpload, xhr, settings); - } else if (typeof FileReader !== undef) { - fileReaderUpload(filesToUpload, xhr, settings); - } else { - $.error('Browser does neither support FormData nor FileReader interface'); - } - } - }, - - handleUpload = function (event, files, input, form, index) { - var xhr = new XMLHttpRequest(), - uploadSettings = $.extend({}, settings); - uploadSettings.fileInput = input; - uploadSettings.uploadForm = form; - if (typeof uploadSettings.initUpload === func) { - uploadSettings.initUpload( - event, - files, - index, - xhr, - uploadSettings, - function () { - upload(files, index, xhr, uploadSettings); - } - ); - } else { - upload(files, index, xhr, uploadSettings); - } - }, - - handleFiles = function (event, files, input, form) { - var i; - if (settings.multiFileRequest) { - handleUpload(event, files, input, form); - } else { - for (i = 0; i < files.length; i += 1) { - handleUpload(event, files, input, form, i); - } - } - }, - - legacyUploadFormDataInit = function (input, form, settings) { - var formData = getFormData(settings); - form.find(':input').not(':disabled') - .attr('disabled', true) - .addClass(settings.namespace + '_disabled'); - $.each(formData, function (index, field) { - $('') - .attr('name', field.name) - .val(field.value) - .addClass(settings.namespace + '_form_data') - .appendTo(form); - }); - input - .attr('name', getFieldName(settings)) - .appendTo(form); - }, - - legacyUploadFormDataReset = function (input, form, settings) { - input.detach(); - form.find('.' + settings.namespace + '_disabled') - .removeAttr('disabled') - .removeClass(settings.namespace + '_disabled'); - form.find('.' + settings.namespace + '_form_data').remove(); - }, - - legacyUpload = function (input, form, iframe, settings) { - var originalAction = form.attr('action'), - originalMethod = form.attr('method'), - originalTarget = form.attr('target'); - iframe - .unbind('abort') - .bind('abort', function (e) { - iframe.readyState = 0; - // javascript:false as iframe src prevents warning popups on HTTPS in IE6 - // concat is used here to prevent the "Script URL" JSLint error: - iframe.unbind('load').attr('src', 'javascript'.concat(':false;')); - if (typeof settings.onAbort === func) { - settings.onAbort(e, [{name: input.val(), type: null, size: null}], 0, iframe, settings); - } - }) - .unbind('load') - .bind('load', function (e) { - iframe.readyState = 4; - if (typeof settings.onLoad === func) { - settings.onLoad(e, [{name: input.val(), type: null, size: null}], 0, iframe, settings); - } - // Fix for IE endless progress bar activity bug (happens on form submits to iframe targets): - $('').appendTo(form).remove(); - }); - form - .attr('action', getUrl(settings)) - .attr('method', getMethod(settings)) - .attr('target', iframe.attr('name')); - legacyUploadFormDataInit(input, form, settings); - iframe.readyState = 2; - form.get(0).submit(); - legacyUploadFormDataReset(input, form, settings); - form - .attr('action', originalAction) - .attr('method', originalMethod) - .attr('target', originalTarget); - }, - - handleLegacyUpload = function (event, input, form) { - // javascript:false as iframe src prevents warning popups on HTTPS in IE6: - var iframe = $(''), - uploadSettings = $.extend({}, settings); - uploadSettings.fileInput = input; - uploadSettings.uploadForm = form; - iframe.readyState = 0; - iframe.abort = function () { - iframe.trigger('abort'); - }; - iframe.bind('load', function () { - iframe.unbind('load'); - if (typeof uploadSettings.initUpload === func) { - uploadSettings.initUpload( - event, - [{name: input.val(), type: null, size: null}], - 0, - iframe, - uploadSettings, - function () { - legacyUpload(input, form, iframe, uploadSettings); - } - ); - } else { - legacyUpload(input, form, iframe, uploadSettings); - } - }).appendTo(form); - }, - - initUploadForm = function () { - uploadForm = (container.is('form') ? container : container.find('form')) - .filter(settings.uploadFormFilter); - }, - - initFileInput = function () { - fileInput = uploadForm.find('input:file') - .filter(settings.fileInputFilter); - }, - - replaceFileInput = function (input) { - var inputClone = input.clone(true); - $('
').append(inputClone).get(0).reset(); - input.after(inputClone).detach(); - initFileInput(); - }; - - this.onDocumentDragOver = function (e) { - if (typeof settings.onDocumentDragOver === func && - settings.onDocumentDragOver(e) === false) { - return false; - } - e.preventDefault(); - }; - - this.onDocumentDrop = function (e) { - if (typeof settings.onDocumentDrop === func && - settings.onDocumentDrop(e) === false) { - return false; - } - e.preventDefault(); - }; - - this.onDragOver = function (e) { - if (typeof settings.onDragOver === func && - settings.onDragOver(e) === false) { - return false; - } - var dataTransfer = e.originalEvent.dataTransfer; - if (dataTransfer) { - dataTransfer.dropEffect = dataTransfer.effectAllowed = 'copy'; - } - e.preventDefault(); - }; - - this.onDrop = function (e) { - if (typeof settings.onDrop === func && - settings.onDrop(e) === false) { - return false; - } - var dataTransfer = e.originalEvent.dataTransfer; - if (dataTransfer && dataTransfer.files && isXHRUploadCapable()) { - handleFiles(e, dataTransfer.files); - } - e.preventDefault(); - }; - - this.onChange = function (e) { - if (typeof settings.onChange === func && - settings.onChange(e) === false) { - return false; - } - var input = $(e.target), - form = $(e.target.form); - if (form.length === 1) { - input.data(defaultNamespace + '_form', form); - replaceFileInput(input); - } else { - form = input.data(defaultNamespace + '_form'); - } - if (!settings.forceIframeUpload && e.target.files && isXHRUploadCapable()) { - handleFiles(e, e.target.files, input, form); - } else { - handleLegacyUpload(e, input, form); - } - }; - - this.init = function (options) { - if (options) { - $.extend(settings, options); - optionsReference = options; - } - initUploadForm(); - initFileInput(); - if (container.data(settings.namespace)) { - $.error('FileUpload with namespace "' + settings.namespace + '" already assigned to this element'); - return; - } - container - .data(settings.namespace, fileUpload) - .addClass(settings.cssClass); - settings.dropZone.not(container).addClass(settings.cssClass); - initEventHandlers(); - }; - - this.options = function (options) { - var oldCssClass, - oldDropZone, - uploadFormFilterUpdate, - fileInputFilterUpdate; - if (typeof options === undef) { - return $.extend({}, settings); - } - if (optionsReference) { - $.extend(optionsReference, options); - } - removeEventHandlers(); - $.each(options, function (name, value) { - switch (name) { - case 'namespace': - $.error('The FileUpload namespace cannot be updated.'); - return; - case 'uploadFormFilter': - uploadFormFilterUpdate = true; - fileInputFilterUpdate = true; - break; - case 'fileInputFilter': - fileInputFilterUpdate = true; - break; - case 'cssClass': - oldCssClass = settings.cssClass; - break; - case 'dropZone': - oldDropZone = settings.dropZone; - break; - } - settings[name] = value; - }); - if (uploadFormFilterUpdate) { - initUploadForm(); - } - if (fileInputFilterUpdate) { - initFileInput(); - } - if (typeof oldCssClass !== undef) { - container - .removeClass(oldCssClass) - .addClass(settings.cssClass); - (oldDropZone ? oldDropZone : settings.dropZone).not(container) - .removeClass(oldCssClass); - settings.dropZone.not(container).addClass(settings.cssClass); - } else if (oldDropZone) { - oldDropZone.not(container).removeClass(settings.cssClass); - settings.dropZone.not(container).addClass(settings.cssClass); - } - initEventHandlers(); - }; - - this.option = function (name, value) { - var options; - if (typeof value === undef) { - return settings[name]; - } - options = {}; - options[name] = value; - fileUpload.options(options); - }; - - this.destroy = function () { - removeEventHandlers(); - container - .removeData(settings.namespace) - .removeClass(settings.cssClass); - settings.dropZone.not(container).removeClass(settings.cssClass); - }; - }; - - methods = { - init : function (options) { - return this.each(function () { - (new FileUpload($(this))).init(options); - }); - }, - - option: function (option, value, namespace) { - namespace = namespace ? namespace : defaultNamespace; - var fileUpload = $(this).data(namespace); - if (fileUpload) { - if (typeof option === 'string') { - return fileUpload.option(option, value); - } - return fileUpload.options(option); - } else { - $.error('No FileUpload with namespace "' + namespace + '" assigned to this element'); - } - }, - - destroy : function (namespace) { - namespace = namespace ? namespace : defaultNamespace; - return this.each(function () { - var fileUpload = $(this).data(namespace); - if (fileUpload) { - fileUpload.destroy(); - } else { - $.error('No FileUpload with namespace "' + namespace + '" assigned to this element'); - } - }); - - } - }; - - $.fn.fileUpload = function (method) { - if (methods[method]) { - return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); - } else if (typeof method === 'object' || !method) { - return methods.init.apply(this, arguments); - } else { - $.error('Method ' + method + ' does not exist on jQuery.fileUpload'); - } - }; - -}(jQuery)); \ No newline at end of file diff --git a/WebContent/_del_fileupload/pbar-ani.gif b/WebContent/_del_fileupload/pbar-ani.gif deleted file mode 100644 index 0dfd45b885a2dd69a4c16febc5e886300cdb08e0..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 304064 zcmeI*dtB9p{l{@^R!$Fj=;tbH^+U9+Oa;+O4J+@Zs7Yyr<-CA-L0||;UemlQ2r2@C z_gjJ5y0~g(>!LGT*;;9BS$0{gW}BIt4&bK&vrcZDWsbNjkoz_n|Fg z4{aH7d_(WgH+21cL+?Wsp})KSU%fLT!v3q-@kaf6+;VfbE;sdvZxi>edZ8DFzLebe zrs(d&a)yQeH{rUxtYIU@rsYHrPrGw;M(3DAPy93{dUR^%m|L&x8`n3hTiU46H%!b< zyLn>2TSiPAJECK1%yri`Ovvk$H!f>jTF$WOym9Fn<2&Vbjv1DkIXtaX=%1^<92*n; zWs)60%cAc1r5r^W+}da?bxwnY+QWxj|mm6$KRciGc2!l#`xwZe#qZ{PWQC&BeF+l<&4hEh_3#*!-i+( z=5&sUseYqVf1Ui3jmtXqMj7MVeEFwrGa_?bY~HY}*tTurVo&|#&!?e!<3e|mY}%+%2%@BSArk8j^8zT?T4e=R!s@`TuGh_AtXsb2kO`lqkg z1(rJElvh@6cKVn9SyD&DrlqHiOUuX^A2MtEk7lVOI*rWC9ycuK+R@{N-I*49=jf3M zv0r_$)_=bf|JI>dgfYE^3x#U6uD0W?PApsP- zF1e5Zid~mnNC3sIOD-gUV%H@X5 zoda*bwg1gu|LTp`_w9Y{)nC5yi_W`cFpQlD_2yMFE1-CSysGs$>O5IMFsf_7tEhG zch2ltGw+=-{hn!4r%awS@$S3{cje}c&;DUnX2!Vmv13M$x^rY&>WJaPhW=p49k&lo zxh?tDK?85OIcdO6{rmMz?9=|EF6i_IzLzw+_ntl!5!oU;D)M(2F~*!yFD^<=kARdZ%c?)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r=A{mL!qySj`T(xuwPAnTHwl}nI+RN; zB!FVqB^MGvvFnlx382_@$%O<^?7HMa0w{J}av=c}yDqtq0E%6gTu1=Lu1hW?fMVAr z7ZO0R>yirzpxAZEg#=LSy5vFvD0W?PApsP-F1e5Zid~mnNC3sIOD-gUV%H@X5P)G=)aelDa{t!m5e?E4TE5WiK>c9{T5Ug4wZHMnkI=a-KBlbC$&b*f z{HWziV;)b;+;&smxjT~{YC3mh<+h?-&(&KL9n9I1@WA#X8z$vW%^jZ`6_|F<>^bui zvuDrE4F+=qfyl*6vzC=+Eh#Q3TfHJ>$@-*dSRji>(=f7c>A zx#U6uD0W?PApsP-F1e5Zid~mnNC3sIOD-gUV%H@X5%9@iKoE^0=t6+8@GJi=*ptxk| zvc;tXm#<6-2G=F6Em`Mj4!kt+GzWrtnu8vxb;PwkaqD<(*x&X|!ltSY<&p~tpxAZE zg#=LSy5vFvD0W?PApsP-F1e5Zid~mnNC3sIOD-gUV%H@X5x3>DmJ~rpnX--zK`Zgyebej{1%w1GmnjiYmoaHIYDwYJ-C#_pu>S+$V zH1RYCf_a)#^U-+vU-4HhvXe_LB!FVqB^MGvvFnlx382_@$%O<^?7HMa0w{J}av=c} zyDqtq0E%6gTu1=Lu1hW?fMVAr7ZO0R>yirzpxAZEg#=LSy5vFvD0W?PApsP-F1e5Z zid~mnNC3sIOD-gUV%H@X5jQuery File Upload Demo
- +
diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml index e01e865..70677e1 100644 --- a/nbproject/build-impl.xml +++ b/nbproject/build-impl.xml @@ -668,12 +668,16 @@ exists or setup the property manually. For example like this: + + + + diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties index 24bbf03..ee2d643 100644 --- a/nbproject/genfiles.properties +++ b/nbproject/genfiles.properties @@ -1,8 +1,8 @@ -build.xml.data.CRC32=7b3b34c6 +build.xml.data.CRC32=90ccae25 build.xml.script.CRC32=5238a167 build.xml.stylesheet.CRC32=651128d4@1.36.1.1 # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. -nbproject/build-impl.xml.data.CRC32=7b3b34c6 -nbproject/build-impl.xml.script.CRC32=6be9306d +nbproject/build-impl.xml.data.CRC32=90ccae25 +nbproject/build-impl.xml.script.CRC32=0d6686c6 nbproject/build-impl.xml.stylesheet.CRC32=b7883b1f@1.36.1.1 diff --git a/nbproject/project.properties b/nbproject/project.properties index 4c1b4eb..0079d38 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -44,7 +44,9 @@ jar.compress=false java.source.based=true javac.classpath=\ ${file.reference.commons-fileupload-1.2.2.jar}:\ - ${file.reference.commons-io-1.3.2.jar} + ${file.reference.commons-io-1.3.2.jar}:\ + ${libs.jsonorg.classpath}:\ + ${libs.imgscalr.classpath} # Space-separated list of extra javac options javac.compilerargs= javac.debug=true diff --git a/nbproject/project.xml b/nbproject/project.xml index 27a3bc3..d5df834 100644 --- a/nbproject/project.xml +++ b/nbproject/project.xml @@ -14,6 +14,14 @@ ${file.reference.commons-io-1.3.2.jar} WEB-INF/lib + + ${libs.jsonorg.classpath} + WEB-INF/lib + + + ${libs.imgscalr.classpath} + WEB-INF/lib + diff --git a/src/info/sudr/file/UploadServlet.java b/src/info/sudr/file/UploadServlet.java index 1fb00c3..dbcd93b 100644 --- a/src/info/sudr/file/UploadServlet.java +++ b/src/info/sudr/file/UploadServlet.java @@ -1,63 +1,173 @@ package info.sudr.file; -import java.io.File; -import java.io.IOException; -import java.io.PrintWriter; +import java.awt.image.BufferedImage; +import java.io.*; import java.util.List; - +import javax.imageio.ImageIO; +import javax.servlet.ServletConfig; import javax.servlet.ServletException; +import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; - import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; +import org.imgscalr.Scalr; +import org.json.JSONArray; +import org.json.JSONObject; public class UploadServlet extends HttpServlet { - private static final long serialVersionUID = 1L; - - /** - * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) - * - */ - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - PrintWriter writer = response.getWriter(); - writer.write("call POST with multipart form data"); - } - - /** - * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) - * - */ - @SuppressWarnings("unchecked") - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - if (!ServletFileUpload.isMultipartContent(request)) { - throw new IllegalArgumentException("Request is not multipart, please 'multipart/form-data' enctype for your form."); - } - - ServletFileUpload uploadHandler = new ServletFileUpload(new DiskFileItemFactory()); - PrintWriter writer = response.getWriter(); - response.setContentType("text/plain"); - try { - List items = uploadHandler.parseRequest(request); - for (FileItem item : items) { - if (!item.isFormField()) { - File file = File.createTempFile(item.getName(), ""); - item.write(file); - writer.write("{\"name\":\""+ item.getName() + "\",\"type\":\"" + item.getContentType() + "\",\"size\":\"" + item.getSize() + "\"}"); - break; // assume we only get one file at a time - } - } - } catch (FileUploadException e) { - throw new RuntimeException(e); - } catch (Exception e) { - throw new RuntimeException(e); - } finally { - writer.close(); - } - - } - -} + +// private static final long serialVersionUID = 1L; + private File fileUploadPath; + + @Override + public void init(ServletConfig config) { + fileUploadPath = new File(config.getInitParameter("upload_path")); + } + + /** + * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) + * + */ + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + + if (request.getParameter("getfile") != null + && !request.getParameter("getfile").isEmpty()) { + File file = new File(fileUploadPath, + request.getParameter("getfile")); + if (file.exists()) { + int bytes = 0; + ServletOutputStream op = response.getOutputStream(); + + response.setContentType(getMimeType(file)); + response.setContentLength((int) file.length()); + response.setHeader( "Content-Disposition", "inline; filename=\"" + file.getName() + "\"" ); + + byte[] bbuf = new byte[1024]; + DataInputStream in = new DataInputStream(new FileInputStream(file)); + + while ((in != null) && ((bytes = in.read(bbuf)) != -1)) { + op.write(bbuf, 0, bytes); + } + + in.close(); + op.flush(); + op.close(); + } + } else if (request.getParameter("delfile") != null && !request.getParameter("delfile").isEmpty()) { + File file = new File(fileUploadPath, request.getParameter("delfile")); + if (file.exists()) { + file.delete(); // TODO:check and report success + } + } else if (request.getParameter("getthumb") != null && !request.getParameter("getthumb").isEmpty()) { + File file = new File(fileUploadPath, request.getParameter("getthumb")); + if (file.exists()) { + String mimetype = getMimeType(file); + if (mimetype.endsWith("png") || mimetype.endsWith("jpeg") || mimetype.endsWith("gif")) { + BufferedImage im = ImageIO.read(file); + if (im != null) { + BufferedImage thumb = Scalr.resize(im, 75); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + if (mimetype.endsWith("png")) { + ImageIO.write(thumb, "PNG" , os); + response.setContentType("image/png"); + } else if (mimetype.endsWith("jpeg")) { + ImageIO.write(thumb, "jpg" , os); + response.setContentType("image/jpeg"); + } else { + ImageIO.write(thumb, "GIF" , os); + response.setContentType("image/gif"); + } + ServletOutputStream srvos = response.getOutputStream(); + response.setContentLength(os.size()); + response.setHeader( "Content-Disposition", "inline; filename=\"" + file.getName() + "\"" ); + os.writeTo(srvos); + srvos.flush(); + srvos.close(); + } + } + } // TODO: check and report success + } else { + PrintWriter writer = response.getWriter(); + writer.write("call POST with multipart form data"); + } + } + + /** + * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) + * + */ + @SuppressWarnings("unchecked") + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + if (!ServletFileUpload.isMultipartContent(request)) { + throw new IllegalArgumentException("Request is not multipart, please 'multipart/form-data' enctype for your form."); + } + + ServletFileUpload uploadHandler = new ServletFileUpload(new DiskFileItemFactory()); + PrintWriter writer = response.getWriter(); + response.setContentType("application/json"); + JSONArray json = new JSONArray(); + try { + List items = uploadHandler.parseRequest(request); + for (FileItem item : items) { + if (!item.isFormField()) { + File file = new File(fileUploadPath, item.getName()); + item.write(file); + JSONObject jsono = new JSONObject(); + jsono.put("name", item.getName()); + jsono.put("size", item.getSize()); + jsono.put("url", "upload?getfile=" + item.getName()); + jsono.put("thumbnail_url", "upload?getthumb=" + item.getName()); + jsono.put("delete_url", "upload?delfile=" + item.getName()); + jsono.put("delete_type", "GET"); + json.put(jsono); + } + } + } catch (FileUploadException e) { + throw new RuntimeException(e); + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + writer.write(json.toString()); + writer.close(); + } + + } + + private String getMimeType(File file) { + String mimetype = ""; + if (file.exists()) { +// URLConnection uc = new URL("file://" + file.getAbsolutePath()).openConnection(); +// String mimetype = uc.getContentType(); +// MimetypesFIleTypeMap gives PNG as application/octet-stream, but it seems so does URLConnection +// have to make dirty workaround + if (getSuffix(file.getName()).equalsIgnoreCase("png")) { + mimetype = "image/png"; + } else { + javax.activation.MimetypesFileTypeMap mtMap = new javax.activation.MimetypesFileTypeMap(); + mimetype = mtMap.getContentType(file); + } + } + System.out.println("mimetype: " + mimetype); + return mimetype; + } + + + + private String getSuffix(String filename) { + String suffix = ""; + int pos = filename.lastIndexOf('.'); + if (pos > 0 && pos < filename.length() - 1) { + suffix = filename.substring(pos + 1); + } + System.out.println("suffix: " + suffix); + return suffix; + } +} \ No newline at end of file