PDA

View Full Version : .zip file Upload



moniroh
Jan 2nd, 2006, 06:45 AM
Hi,

I want to create an application to upload .zip files - the file should be unpacked during the upload.

I currently have implemented a simple file upload:

Here my source code:
The Controller:

package controllers;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBin der;
import org.springframework.web.multipart.MultipartHttpSer vletRequest;
import org.springframework.web.multipart.commons.CommonsM ultipartFile;
import org.springframework.web.multipart.support.ByteArra yMultipartFileEditor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormCont roller;

import beans.FileBean;

/**
* Controller zur Realisierung des Upload-Prozesses
*
* */
public class FileUploadController extends SimpleFormController {

private String directory;

protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws ServletException
{
binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
}

protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors)
throws ServletException, IOException {

FileBean fb = (FileBean) command;

byte[] bytes = fb.getFile();

MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("file");

String uploadDir = getServletContext().getRealPath(directory);

File dirPath = new File(uploadDir);

if(!dirPath.exists()) dirPath.mkdirs();

String sep = System.getProperty("file.separator");

File uploadedFile = new File(uploadDir + sep + file.getOriginalFilename());
FileCopyUtils.copy(bytes, uploadedFile);

request.getSession().setAttribute("message", "Upload erfolgreich");

String url = request.getContextPath() + directory + file.getOriginalFilename();

Map model = new HashMap();
model.put("filename", file.getOriginalFilename());
model.put("url", url);


return new ModelAndView(getSuccessView(), "model", model);


}


/**
* @return
*/
public String getDirectory() {
return directory;
}

/**
* @param string
*/
public void setDirectory(String string) {
directory = string;
}

}

The Bean:
public class FileBean {

private byte[] file;

public byte[] getFile() {
return file;
}

public void setFile(byte[] bs) {
file = bs;
}

}[/SIZE]

and the xml file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsM ultipartResolver"/>

<bean id="fileUploadController" class="controllers.FileUploadController">
<property name="commandClass"><value>beans.FileBean</value></property>
<property name="formView"><value>upload</value></property>
<property name="successView"><value>upload</value></property>
<property name="directory"><value>/upload/</value></property>
</bean>

<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlH andlerMapping">
<property name="mappings">
<props>
<prop key="upload.html">fileUploadController</prop>
</props>
</property>
</bean>

<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResou rceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="suffix"><value>.jsp</value></property>
</bean>
</beans>

Has anyone implemented or an idea how to realize an zip file upload?

akram
Jan 31st, 2006, 07:54 AM
I suggest you to upload the file in all cases and the to unzip it in your controller.

If you eant to check if the file is a "real" zip file, you could perform this step in the validator ( by checking the first bytes of the file, for instance) or trying to unzip it and catching an exception.

4had
Feb 1st, 2006, 07:35 AM
@moniroh: Look at http://java.sun.com/developer/technicalArticles/Programming/compression/

I have nearly the same problem. I want to unzip the zip-File in my Validator class, but I don't know how to access getRealPath() of ServletContext. I'm only able to use getServletContext() in my Controller.

My current solution is to use


// Controller

protected Object formBackingObject(HttpServletRequest request)
throws ServletException {
myManager.setSc(this.getServletContext());
return new XBean();
}


and now I'm able to get the ServletContext from myManager in the Validation class. It works, but doesn't look nice!

Does anyone have a better solution for this problem?

Regards,
Farhad