This is kind of an old post but in case someone is interested I mad my own
implementation of the CosMultipartRequestFilter to deal with larger files. The
implementation uses Apache Commons FileUpload class to handle the file uploads:
[code]
public class MultipartFilter extends OncePerRequestAbstractMgnlFilter {
@Override
public void doFilter (HttpServletRequest request, HttpServletResponse
response, FilterChain chain) throws IOException, ServletException {
String type = null;
String type1 = request.getHeader("Content-Type");
String type2 = request.getContentType();
if (type1 == null && type2 != null) {
type = type2;
}
else if (type2 == null && type1 != null) {
type = type1;
}
else if (type1 != null) {
type = (type1.length() > type2.length() ? type1 : type2);
}
boolean isMultipart = (type != null) &&
type.toLowerCase().startsWith("multipart/form-data");
MultipartForm mpf = null;
try{
if (isMultipart) {
mpf = parseParameters(request);
request = new MultipartRequestWrapper(request, mpf);
MgnlContext.push(request, response);
}
try {
chain.doFilter(request, response);
} finally {
if (isMultipart) {
MgnlContext.pop();
}
}
} finally {
if (mpf != null) {
Iterator<Document> keys =
mpf.getDocuments().values().iterator();
while (keys.hasNext()) {
Document doc = keys.next();
if (doc != null && doc.getFile() != null &&
doc.getFile().exists()) {
doc.delete();
}
}
}
}
}
/**
* Adds all request paramaters as request attributes.
* @param request HttpServletRequest
* @throws FileUploadException
*/
private static MultipartForm parseParameters(HttpServletRequest request)
throws IOException {
try {
MultipartForm form = new MultipartForm();
String encoding =
StringUtils.defaultString(request.getCharacterEncoding(), "UTF-8");
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory ();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload (factory);
// Parse the request
List<FileItem> items = upload.parseRequest(request);
Enumeration params = request.getParameterNames();
while (params.hasMoreElements()) {
String name = (String) params.nextElement();
String value = request.getParameter(name);
form.addParameter(name, value);
String[] s = request.getParameterValues(name);
if (s != null) {
form.addparameterValues(name, s);
}
}
Iterator<FileItem> iterator = items.iterator();
while (iterator.hasNext ()) {
FileItem next = iterator.next();
if (next.isFormField()) {
if (next.getFieldName () != null) {
form.addParameter(next.getFieldName(),
next.getString());
}
continue;
}
String name = next.getName();
File tempDir = Files.createTempDir();
File tempFile = new File (tempDir.getAbsolutePath() + "\\" +
name);
FileOutputStream output = new FileOutputStream(tempFile);
try {
IOUtils.copy (next.getInputStream (), output);
}
finally {
try {
output.flush();
}
finally {
output.close();
}
}
form.addDocument (next.getFieldName(), next.getName(),
next.getContentType(), tempFile);
}
request.setAttribute(MultipartForm.REQUEST_ATTRIBUTE_NAME, form);
return form;
}
catch (FileUploadException e) {
throw new RuntimeException (e);
}
}
}[/code]
HTH,
Arik
--
Context is everything:
http://forum.magnolia-cms.com/forum/thread.html?threadId=85c86faf-a339-4be0-8b72-361499a1a056
----------------------------------------------------------------
For list details, see http://www.magnolia-cms.com/community/mailing-lists.html
Alternatively, use our forums: http://forum.magnolia-cms.com/
To unsubscribe, E-mail to: <[email protected]>
----------------------------------------------------------------