Upload file in struts

Why Uploading File

Web based application often need client to upload file, doc file (word file), PDF file, images file to a server. This uploading file can be server side or client side. In client side, user can browser a file and upload to server, same in server side, but no dynamic location (Fix URL) and URL path of uploading file is static.

Need of Upload file

Uploading file is basic requirement of any application, every project, java application needs an uploading file option. In house project, need to share doc files, PDF file etc, Emailing application using huge uploading file option. Uploading file in java, need multipart form data request with post method. All requests send to server in post method with ENCTYPE type of multipart form data.

To process the multipart request, we need API which helps us to process this request. This request is usually different from normal request which send to server.

Java can get this request for further process of uploading to particular folder or location.

Request comes from HTML page in binary form, can be easily converted into File object of java and save as it file with same name or different name.

Struts provide easiest way to upload file to server by user.

All multipart/form data request is handled by apache file upload jar. Struts is project from apache, so apache use own API. Instead of apache file upload API, can use orielly multipart API, us available by servlet.com Uploading file in Struts Java

index.jsp

<%@ page language="java" import="java.util.*"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<html>
<head>
<title>Upload a file</title>
</head>

<body>
<html:form action="/upload" enctype="multipart/form-data">
Upload file <html:errors/><br />
<html:file property="uFile" /> <br/>
<html:submit property="submit">Upload</html:submit>
</html:form>
</body>
</html>

struts-config.xml

<struts-config>
<data-sources />
<form-beans >
<form-bean name="upload" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="uFile" type="org.apache.struts.upload.FormFile" />
<form-property name="submit" type="java.lang.String" />
</form-bean>

</form-beans>
<global-exceptions />
<global-forwards>
<forward name="cancel" path="/index.jsp" redirect="true"/>
</global-forwards>

<action-mappings>

<action
input="input"
path="/upload"
type="com.upload.fileAction.UploadFileAction"
name="upload"
scope="request"
validate="false">
<forward name="input" path="/index.jsp" />
<forward name="success" path="/index.jsp" />
</action>
</action-mappings>

<controller inputForward="true" />
<message-resources parameter="com.upload.struts.ApplicationResources" />

</struts-config>

UploadFileAction.java

package com.upload.fileAction;

import java.io.File;
import java.io.FileOutputStream;

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

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.upload.FormFile;

public class UploadFileAction extends Action{

public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {

boolean errorFlag=false;

DynaActionForm uploadForm = (DynaActionForm) form;

ActionMessages errors = new ActionMessages();

FormFile uploadFile   = (FormFile)uploadForm.get("uFile");

String uploadingFileName    = uploadFile.getFileName();

String uploadPath=getServlet().getServletContext().getRealPath("/") +"/upload";

if(!uploadingFileName.equals("")){

File uploadFileObject = new File(uploadPath, uploadingFileName);

FileOutputStream fileOutStream = new FileOutputStream(uploadFileObject);
fileOutStream.write(uploadFile.getFileData());
fileOutStream.flush();
fileOutStream.close();
}
else
{
errorFlag=true;
}

if(errorFlag==true)
{
errors.add("submitError",new ActionMessage("error.save.saveError"));
saveErrors(request,errors);
return (mapping.findForward("success"));
}
else {
errors.add("submitSuccess",new ActionMessage("error.save.success"));
saveErrors(request,errors);
return (mapping.findForward("success"));
}
}
}

ApplicationResources.properties

error.save.success=File is uploaded successfully
error.save.update=Data is updated successfully
error.save.saveError=Error: File is not uploaded
Bookmark  

 

7 Responses to “Upload file in struts”

  1. Venkat Reddy Kandhla says:

    hi when i executed this i got error like this
    java.io.FileNotFoundException: C:\bea\user_projects\workspaces\workSpaceStudio\venki2\build\weboutput\upload\anushka-sharma-2v.jpg (The system cannot find the path specified)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.(FileOutputStream.java:179)
    at java.io.FileOutputStream.(FileOutputStream.java:131)

  2. Venkat Reddy Kandhla says:

    the system cannot find the path specified ,how i am able to get rid of this error

  3. Jeshurun says:

    Did you create a folder named upload inside your project’s root directory?

  4. chinni says:

    Really i don\\\’t know abt file uploading in struts.
    This example helps me alot.

  5. Kumaran says:

    please check in your server whether this folder exists or not C:\bea\user_projects\workspaces\workSpaceStudio\venki2\build\weboutput\upload\

  6. sekhar says:

    i want to upload a pdf file into the database plzz help me..

  7. Hi,

    when we create file input type in HTML we can able to get Full file path for example my file in c:/temp/sample.jpg means, in IE6, it gets full path but in IE 7,IE8 or Mozila browsers only gets file name only example sample.jpg, so only you faced this error.

    Thank You

Leave a Reply

Security Code:

 

  Random Post