1

I would like to get Coding To Upload Image(.jpg)/Photo to Server Machine using Struts1.x and
mySQL database I have the code for File Upload Instead.

Let me know what tweaking is required here. TC

Code for File Upload :----

public class FileUploadForm extends ActionForm{

private FormFile file;

public FormFile getFile() {
    return file;
}

public void setFile(FormFile file) {
    this.file = file;
}

@Override
public ActionErrors validate(ActionMapping mapping,
   HttpServletRequest request) {

    ActionErrors errors = new ActionErrors();

    if( getFile().getFileSize()== 0){
       errors.add("common.file.err",
        new ActionMessage("error.common.file.required"));
       return errors;
    }

    //only allow textfile to upload
    if(!"text/plain".equals(getFile().getContentType())){
        errors.add("common.file.err.ext",
         new ActionMessage("error.common.file.textfile.only"));
        return errors;
    }

        //file size cant larger than 10kb
    System.out.println(getFile().getFileSize());
    if(getFile().getFileSize() > 10240){ //10kb
       errors.add("common.file.err.size",
        new ActionMessage("error.common.file.size.limit", 10240));
       return errors;
    }

    return errors;
}

}

Action Class:--

public class FileUploadAction extends Action{

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

    FileUploadForm fileUploadForm = (FileUploadForm)form;

    FormFile file = fileUploadForm.getFile();

    //Get the servers upload directory real path name
    String filePath = 
           getServlet().getServletContext().getRealPath("/") +"upload";

    //create the upload folder if not exists
    File folder = new File(filePath);
    if(!folder.exists()){
        folder.mkdir();
    }

    String fileName = file.getFileName();

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

        System.out.println("Server path:" +filePath);
        File newFile = new File(filePath, fileName);

        if(!newFile.exists()){
          FileOutputStream fos = new FileOutputStream(newFile);
          fos.write(file.getFileData());
          fos.flush();
          fos.close();
        }  

        request.setAttribute("uploadedFilePath",newFile.getAbsoluteFile());
        request.setAttribute("uploadedFileName",newFile.getName());
    }
    return mapping.findForward("success");
}

}

Thanks in Advance!

2
  • the above code works fine. TC . I have verified
    – ACJ
    Feb 5, 2014 at 6:19
  • Refere this link for Image/Photo Upload in struts1.x roseindia.net/struts/strutsfileuploadandsave.shtml Now , I am trying for multiple files upload in Single go using tweaking this code. will keep posted on the same. Tc & Rgds ACJ
    – ACJ
    Feb 5, 2014 at 6:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.