some cleanup
This commit is contained in:
parent
2234202c69
commit
6c05cfda13
1 changed files with 56 additions and 53 deletions
|
|
@ -29,6 +29,7 @@ import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
||||||
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
||||||
import org.apache.commons.fileupload.util.Streams;
|
import org.apache.commons.fileupload.util.Streams;
|
||||||
import zutil.StringUtil;
|
import zutil.StringUtil;
|
||||||
|
import zutil.io.MultiPrintStream;
|
||||||
import zutil.io.file.FileUtil;
|
import zutil.io.file.FileUtil;
|
||||||
import zutil.jee.upload.FileUploadListener.Status;
|
import zutil.jee.upload.FileUploadListener.Status;
|
||||||
import zutil.log.LogUtil;
|
import zutil.log.LogUtil;
|
||||||
|
|
@ -78,8 +79,8 @@ import java.util.logging.Logger;
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* </pre>
|
* </pre>
|
||||||
* @author Ziver
|
|
||||||
*
|
*
|
||||||
|
* @author Ziver
|
||||||
*/
|
*/
|
||||||
public abstract class AjaxFileUpload extends HttpServlet {
|
public abstract class AjaxFileUpload extends HttpServlet {
|
||||||
private static final Logger logger = LogUtil.getLogger();
|
private static final Logger logger = LogUtil.getLogger();
|
||||||
|
|
@ -113,13 +114,10 @@ public abstract class AjaxFileUpload extends HttpServlet {
|
||||||
|
|
||||||
// Read allowed file types
|
// Read allowed file types
|
||||||
if (config.getInitParameter("ALLOWED_EXTENSIONS") != null) {
|
if (config.getInitParameter("ALLOWED_EXTENSIONS") != null) {
|
||||||
String[] tmp = config.getInitParameter("ALLOWED_EXTENSIONS").split(",");
|
ALLOWED_EXTENSIONS.addAll(Arrays.asList(
|
||||||
StringBuilder ext_log = new StringBuilder("Allowed extensions: ");
|
config.getInitParameter("ALLOWED_EXTENSIONS").toLowerCase().split(",")));
|
||||||
for( String ext : tmp ){
|
|
||||||
ALLOWED_EXTENSIONS.add(ext.trim().toLowerCase());
|
logger.info("Allowed extensions: " + MultiPrintStream.dumpToString(ALLOWED_EXTENSIONS));
|
||||||
ext_log.append(ext).append(", ");
|
|
||||||
}
|
|
||||||
logger.info( ext_log.toString() );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
@ -170,7 +168,6 @@ public abstract class AjaxFileUpload extends HttpServlet {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
protected void doPost(HttpServletRequest request,
|
protected void doPost(HttpServletRequest request,
|
||||||
HttpServletResponse response) throws ServletException, IOException {
|
HttpServletResponse response) throws ServletException, IOException {
|
||||||
|
|
@ -205,18 +202,23 @@ public abstract class AjaxFileUpload extends HttpServlet {
|
||||||
FileItemIterator it = upload.getItemIterator(request);
|
FileItemIterator it = upload.getItemIterator(request);
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
FileItemStream item = it.next();
|
FileItemStream item = it.next();
|
||||||
|
|
||||||
// Is the file type allowed?
|
// Is the file type allowed?
|
||||||
if( !item.isFormField() && !ALLOWED_EXTENSIONS.contains( FileUtil.getFileExtension(item.getName()).toLowerCase() )){
|
String ext = FileUtil.getFileExtension(item.getName()).toLowerCase();
|
||||||
String msg = "Filetype '"+FileUtil.getFileExtension(item.getName())+"' is not allowed!";
|
if (!item.isFormField() &&
|
||||||
|
!ALLOWED_EXTENSIONS.contains(ext)) {
|
||||||
|
String msg = "File type '" + ext + "' is not allowed!";
|
||||||
logger.warning(msg);
|
logger.warning(msg);
|
||||||
listener.setStatus(Status.Error);
|
listener.setStatus(Status.Error);
|
||||||
listener.setFileName(item.getName());
|
listener.setFileName(item.getName());
|
||||||
listener.setMessage(msg);
|
listener.setMessage(msg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
listener.setFileName(item.getName());
|
listener.setFileName(item.getName());
|
||||||
FileItem fileItem = factory.createItem(item.getFieldName(),
|
FileItem fileItem = factory.createItem(item.getFieldName(),
|
||||||
item.getContentType(), item.isFormField(), item.getName());
|
item.getContentType(), item.isFormField(), item.getName());
|
||||||
|
|
||||||
// Read the file data
|
// Read the file data
|
||||||
Streams.copy(item.openStream(), fileItem.getOutputStream(), true);
|
Streams.copy(item.openStream(), fileItem.getOutputStream(), true);
|
||||||
if (fileItem instanceof FileItemHeadersSupport) {
|
if (fileItem instanceof FileItemHeadersSupport) {
|
||||||
|
|
@ -227,15 +229,16 @@ public abstract class AjaxFileUpload extends HttpServlet {
|
||||||
//Handle the item
|
//Handle the item
|
||||||
if (fileItem.isFormField()) {
|
if (fileItem.isFormField()) {
|
||||||
fields.put(fileItem.getFieldName(), fileItem.getString());
|
fields.put(fileItem.getFieldName(), fileItem.getString());
|
||||||
}
|
} else {
|
||||||
else{
|
|
||||||
files.add(fileItem);
|
files.add(fileItem);
|
||||||
logger.info("Recieved file: "+fileItem.getName()+" ("+StringUtil.formatByteSizeToString(fileItem.getSize())+")");
|
logger.info("Received file: " + fileItem.getName() + " (" + StringUtil.formatByteSizeToString(fileItem.getSize()) + ")");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process the upload
|
// Process the upload
|
||||||
listener.setStatus(Status.Processing);
|
listener.setStatus(Status.Processing);
|
||||||
doUpload(request, response, fields, files);
|
doUpload(request, response, fields, files);
|
||||||
|
|
||||||
// Done
|
// Done
|
||||||
listener.setStatus(Status.Done);
|
listener.setStatus(Status.Done);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue