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