Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Upload a file with FormFile using Struts

843830Aug 17 2006
Hi, I've uploaded a file using Struts with Form File.

/**
* UploadForm.java
*/
public class UploadForm extends ActionForm {

/**
* The file that the user has uploaded
*/
private FormFile theFile;

/**
* The file path to write to
*/
protected String filePath;

/**
* Retrieve a representation of the file the user has uploaded
*/
public FormFile getTheFile() {
return theFile;
}

/**
* Set a representation of the file the user has uploaded
*/
public void setTheFile(FormFile theFile) {
this.theFile = theFile;
}
}


/*
* UploadAction.java
*/
public class UploadAction extends Action
{
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

if (form instanceof UploadForm) {
UploadForm theForm = (UploadForm) form;

// mostramos los parametros del fichero
FormFile theFile = theForm.getTheFile();
String contentType = theFile.getContentType();
String fileName = theFile.getFileName();
int fileSize = theFile.getFileSize();
byte[] fileData = theFile.getFileData();
System.out.println("Tipo: " + contentType);
System.out.println("Nombre: " + fileName);
System.out.println("Tamano: " + fileSize);

try {
//guarda los datos del fichero
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream stream = file.getInputStream();

// solo si el archivo es de menos de 4MB
if (fileSize() < (4*1024000)) {
byte[] buffer = new byte[8192];
int bytesLeidos = 0;
while ((bytesLeidos = stream.read(buffer, 0, 8192)) != -1) {
baos.write(buffer, 0, bytesLeidos );
}
data = new String(baos.toByteArray());
}
else {
data = new String("Fichero de m�s de 4MB: no pudo almacenarse." +
" Tamano del fichero: " + fileSize() + " bytes.");
}
}

return mapping.findForward("success");
}
// solo si no lee un UploadForm
return null;
}

/*
config-struts.xml
*/
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>

<form-beans>
<form-bean name="uploadForm" type="autentia.UploadForm" />
</form-beans>

<action-mappings>
<action path="/upload" forward="/upload.jsp" />
<!-- Upload Action -->
<action path="/upload-submit"
type="autentia.UploadAction"
name="uploadForm" scope="request" input="input">
<forward name="input" path="/upload.jsp" />
<forward name="display" path="/display.jsp" />
</action>
</action-mappings>

<!-- This is where you set the maximum file size for your file uploads.
-1 by default: unlimited size. -->
<controller maxFileSize="4M" inputForward="true" />
<message-resources parameter="autentia.UploadResources"/>

</struts-config>

Now, I need to download that file from Server to the client machine.

How can i do that???

Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 14 2006
Added on Aug 17 2006
0 comments
505 views