Announcement Announcement Module
Collapse

Spring Modules forum decommissioned in favor of Spring Extensions

As the Spring Modules project has been replaced by the Spring Extensions (http://www.springsource.org/extensions) project, this forum has been decommissioned in favour of Spring Extensions one at:
http://forum.springsource.org/forumdisplay.php?f=44

Please see the Spring Extensions home page for a complete list of current projects in Java, .NET and ActionScript. You can also propose one if you want.

Cheers,
Costin Leau
SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
http://twitter.com/costinl
See more
See less
Multiple Files Uploading Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Multiple Files Uploading

    Hello Friends,

    Untill now i was trying the example given in the Spring Document for file uploading. Its working fine.

    I want to know that Is there is another class which supports for Uploading Multiple files from the page.

    Thanks all

    Aski

  • #2
    There is nothing "special" about uploading more than one file. What problems are you having?

    Comment


    • #3
      I want to upload multiple files(all kind of files like .jpg,.pdf.dat,etc) at a single time to the specified location in the Server.

      Aski

      Comment


      • #4
        Have you tried to upload more than one file?

        Comment


        • #5
          Originally posted by Aski View Post
          I want to upload multiple files(all kind of files like .jpg,.pdf.dat,etc) at a single time to the specified location in the Server.

          Aski
          Hi All,
          I'm new to spring and file uploading part.
          Please tell me how you save an image file on server at some specific location.
          And how you can view these files from jsp.
          I'm using struts for my view.
          Please suggest some tutorial also.
          Thanks

          Comment


          • #6
            Originally posted by rupesh_dce View Post
            Please tell me how you save an image file on server at some specific location.
            That's what I was wondering.
            I wan't to upload a file, and place it in my WebContent. But can I do it without injecting the path, can I locate it from my controller without injecting it?

            Thx
            Compi

            Comment


            • #7
              Me too having same problem.

              File uploading for single file works just fine. I tried to upload more than 1 file, b I was not successful.

              Please see my code below.

              In my recruiting-servlet.xml -------

              <bean id="multipartResolver"
              class="org.springframework.web.multipart.commons.C ommonsMultipartResolver">
              <property name="maxUploadSize" value="100000"/>
              </bean>

              <bean id="candidateEditController" class="com.wolfram.spring.CandidateEditController" >
              <property name="commandClass" value="com.wolfram.webapps.recruiting.FileUploadBe an" />

              <property name="successView" value="Candidate.html" />
              <property name="candidateDao" ref="candidateDao" />
              <property name="skillsDao" ref="skillsDao" />
              <property name="positionDao" ref="positionDao" />
              <property name="userDao" ref="userDao" />
              <property name="velocityEngine" ref="velocityEngine" />
              <property name="mailSender" ref="mailSender" />


              <property name="redirectOnSuccess" value="true" />
              <property name="redirectProperties">
              <list>
              <value>id</value>
              </list>
              </property>
              </bean>
              -----------------------------
              In FileUploadBean.java--------------

              package com.wolfram.webapps.recruiting;

              import org.springframework.web.multipart.MultipartFile;

              public class FileUploadBean
              {

              private MultipartFile file;

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

              public MultipartFile getFile()
              {
              return file;
              }

              private boolean mTooBig;

              public void setTooBig(boolean aTooBig)
              {
              mTooBig = aTooBig;
              }

              public boolean isTooBig()
              {
              return mTooBig;
              }
              }

              -------------------------------
              In CandidateEditController.java ------


              private int processUploadedFile(HttpServletRequest request, Object command, String id)
              {
              logger.trace("CandidateEditController:rocessUplo adedFile():Entering...");

              // Setting the command object as FileUploadBean
              FileUploadBean bean = (FileUploadBean) command;

              MultipartFile file = bean.getFile();

              String contenttype = file.getContentType();
              if(file.isEmpty())
              {
              logger.trace("CandidateEditController:rocessUplo adedFile():File is not attached, exiting with 1 ");
              return 1;
              }

              if (!contenttype.equals("application/octet-stream"))
              {
              if (logger.isDebugEnabled())
              logger.debug("CandidateEditController:rocessUplo adedFile(): contenttype:= " + contenttype);
              if (file == null)
              {
              // hmm, that's strange, the I did not upload anything
              if (logger.isDebugEnabled())
              logger.debug("CandidateEditController:rocessUplo adedFile():File is not uploaded.");
              }

              String itemName = file.getOriginalFilename();

              String fileLocation = getServletContext().getRealPath("/") + "uploadedFiles";

              if (logger.isDebugEnabled())
              logger.debug("CandidateEditController:rocessUplo adedFile():File uploaded is " + file.getOriginalFilename());

              File ff = new File(fileLocation);
              if (ff.mkdir())
              if (logger.isDebugEnabled())
              try
              {
              if (logger.isDebugEnabled())
              logger.debug("CandidateEditController:rocessUplo adedFile():New directory is created at.." + ff.getCanonicalPath());
              } catch (IOException e)
              {
              // TODO Auto-generated catch block
              e.printStackTrace();
              }

              // Saving uploaded file to local disk location.
              String filePath = fileLocation + "\\" + itemName;

              File f = new File(filePath);

              if (!f.exists())
              try
              {
              f.createNewFile();
              file.transferTo(f);
              } catch (IOException e)
              {
              // TODO Auto-generated catch block
              e.printStackTrace();
              }
              // candidate.setResumeFile(resumeFile)

              /**
              * save File to the database
              *
              */
              candidateDao.saveFileToDB(f, id, filePath);
              logger.trace("CandidateEditController:rocessUplo adedFile(): File processed, Exiting with 0 ");
              return 0;
              } else
              {
              logger.trace("CandidateEditController:rocessUplo adedFile(): Executable File can not be processed, Exiting with -1 ");
              return -1;
              }
              }

              --------------------
              This Works pretty good.
              But I am not sure how would I modify or write new code to upload/process multiple files.

              for multiple files uploading I changed FileUploadBean to use array of MultipartFile. and tried to access it into method ProcessFileUpload() in CandiddateEditController. java

              But that didnt work. Please Let me know how do I fix it. That would be really great help.

              --------------------------------------------------------------

              ***************Using apache-Commons file upload :-

              -- Didnt use FileUploadBean, used only simpleFormController with Candidate as Commnad class.
              -- And used following Code:-

              private String processFileUpload(HttpServletRequest request,
              ServletResponse response) {
              logger.debug("ParticipateDataFilter:rocessFileUp load() entering ");

              // if file is successfully uploaded save this into the directory.

              boolean isMultipart = ServletFileUpload.isMultipartContent(request);
              String filePath = null;
              if (!isMultipart) {
              if (logger.isDebugEnabled())
              logger
              .debug("ParticipateDataFilter:rocessFileUpload() Request IS NOT multipart ");
              } else {

              if (logger.isDebugEnabled())
              logger
              .debug("ParticipateDataFilter:rocessFileUpload() Request IS multipart ");
              FileItemFactory factory = new DiskFileItemFactory();

              ServletFileUpload upload = new ServletFileUpload(factory);

              List items = null;
              try {

              items = upload.parseRequest(request);
              if (logger.isDebugEnabled())
              logger
              .debug("ParticipateDataFilter:rocessFileUpload() fileItems size "
              + items.size());
              } catch (Exception e) {
              logger
              .debug("ParticipateDataFilter:rocessFileUpload() Excpetion in Uploadig request is "
              + e.getMessage());
              }
              Iterator itr = items.iterator();
              while (itr.hasNext()) {
              FileItem item = (FileItem) itr.next();
              if (item.isFormField()) {
              if (logger.isDebugEnabled())
              logger
              .debug("ParticipateDataFilter:rocessFileUpload() : This is form field ");
              String paramName = item.getFieldName();
              String paramValue = item.getString();

              /*logger
              .warn("ParticipateDataFilter:rocessFileUpload(): paramName :- "
              + paramName
              + " paramValue :- "
              + paramValue);*/

              request.setAttribute(paramName, paramValue);
              } else {
              if (logger.isDebugEnabled())
              logger
              .debug("ParticipateDataFilter:rocessFileUpload() This is NOT form field ");

              try {
              String itemName = item.getName(); // String itemName
              // =file.
              // getOriginalFilename
              // ();
              String fileLocation = "/www/tomcat/data/Calculate/Participate";

              File ff = new File(fileLocation);
              if (ff.mkdir())
              if (logger.isDebugEnabled())
              logger
              .debug("ParticipateDataFilter:rocessFileUpload() Directory path here "
              + ff.getCanonicalPath());

              // Saving uploaded file to local disk location.
              filePath = fileLocation + "/" + itemName;
              File savedFile = new File(filePath);
              if (logger.isDebugEnabled())
              logger
              .debug("ParticipateDataFilter:rocessFileUpload() File path here "
              + filePath);
              if (!savedFile.exists())
              savedFile.createNewFile();
              item.write(savedFile);

              // Kirti:- uncomment this code to restrict upload file
              // to 1MB
              long size = item.getSize();
              if (size > 1000000) {
              request
              .setAttribute("message",
              "File cant be uploaded due to maxium size limit");
              // item.delete();
              savedFile.delete(); // delete file if it exceeds
              // maximum size limit.
              logger
              .warn("ParticipateDataFilter:rocessFileUpload(): File cant be uploaded due to maxium size limit");
              request.setAttribute("isEmailSent", "false");
              } else {
              logger
              .warn("ParticipateDataFilter:rocessFileUpload(): File is successfully uploaded");
              request.setAttribute("isEmailSent", "true");
              }
              request.setAttribute("filename", itemName);
              request.setAttribute("fileSize", new Long(size));
              } catch (Exception e) {
              logger
              .debug("ParticipateDataFilter:rocessFileUpload() Exception in file uploading "
              + e.getMessage());
              }
              }
              }
              }
              return filePath;
              }



              -----At the Line :--- items = upload.parseRequest(request); items.size() is Zero. So doesn't do anything further.


              Please Let me know how do I fix it. That would be really great help. I would appreciate if you can share any working example with me.

              Comment

              Working...
              X