Results 1 to 7 of 7

Thread: Multiple Files Uploading

  1. #1
    Join Date
    Nov 2005
    Posts
    4

    Post 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. #2
    Join Date
    Aug 2004
    Posts
    1,905

    Default

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

  3. #3
    Join Date
    Nov 2005
    Posts
    4

    Default

    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

  4. #4
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Have you tried to upload more than one file?

  5. #5
    Join Date
    Oct 2006
    Posts
    2

    Default

    Quote 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

  6. #6
    Join Date
    Jul 2006
    Posts
    7

    Default

    Quote 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

  7. #7

    Default

    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::processUplo 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::processUplo adedFile():File is not attached, exiting with 1 ");
    return 1;
    }

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

    String itemName = file.getOriginalFilename();

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

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

    File ff = new File(fileLocation);
    if (ff.mkdir())
    if (logger.isDebugEnabled())
    try
    {
    if (logger.isDebugEnabled())
    logger.debug("CandidateEditController::processUplo 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::processUplo adedFile(): File processed, Exiting with 0 ");
    return 0;
    } else
    {
    logger.trace("CandidateEditController::processUplo 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::processFileUp 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::processFileUpload() Request IS NOT multipart ");
    } else {

    if (logger.isDebugEnabled())
    logger
    .debug("ParticipateDataFilter::processFileUpload() 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::processFileUpload() fileItems size "
    + items.size());
    } catch (Exception e) {
    logger
    .debug("ParticipateDataFilter::processFileUpload() 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::processFileUpload() : This is form field ");
    String paramName = item.getFieldName();
    String paramValue = item.getString();

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

    request.setAttribute(paramName, paramValue);
    } else {
    if (logger.isDebugEnabled())
    logger
    .debug("ParticipateDataFilter::processFileUpload() 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::processFileUpload() 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::processFileUpload() 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::processFileUpload(): File cant be uploaded due to maxium size limit");
    request.setAttribute("isEmailSent", "false");
    } else {
    logger
    .warn("ParticipateDataFilter::processFileUpload(): File is successfully uploaded");
    request.setAttribute("isEmailSent", "true");
    }
    request.setAttribute("filename", itemName);
    request.setAttribute("fileSize", new Long(size));
    } catch (Exception e) {
    logger
    .debug("ParticipateDataFilter::processFileUpload() 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •