DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2

Hybrid View

  1. #1
    Join Date
    Jan 2006
    Location
    New York
    Posts
    9

    Unable To Write Image File on Server Directory

    I am trying to write and image file in my server hosting directory, but it is throwing exception, same code works fine when i run it on my laptop locally, it uploads the image on my laptop windows directory but throws an exception when i upload my application on server and tries to write image file on the server directory. Following is the exception it throws...Please guide me what i am doing wrong...Please Note that I am using Apache FileUpload to write image file on the server directory

    HTTP Status 500 -

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

    type Exception report

    message

    description The server encountered an internal error () that prevented it from fulfilling this request.

    exception

    java.security.AccessControlException: access denied (java.io.FilePermission /web/tomcat/temp/upload__10c3edf7_1094f5d6d15__7ffe_00000002.tmp write)
    java.security.AccessControlContext.checkPermission (AccessControlContext.java:264)
    java.security.AccessController.checkPermission(Acc essController.java:427)
    java.lang.SecurityManager.checkPermission(Security Manager.java:532)
    java.lang.SecurityManager.checkWrite(SecurityManag er.java:962)
    java.io.FileOutputStream.<init>(FileOutputStream.java:169)
    java.io.FileOutputStream.<init>(FileOutputStream.java:131)
    org.apache.commons.io.output.DeferredFileOutputStr eam.thresholdReached(DeferredFileOutputStream.java :122)
    org.apache.commons.io.output.ThresholdingOutputStr eam.checkThreshold(ThresholdingOutputStream.java:2 21)
    org.apache.commons.io.output.ThresholdingOutputStr eam.write(ThresholdingOutputStream.java:128)
    org.apache.commons.fileupload.MultipartStream.read BodyData(MultipartStream.java:520)
    org.apache.commons.fileupload.FileUploadBase.parse Request(FileUploadBase.java:371)
    org.apache.commons.fileupload.servlet.ServletFileU pload.parseRequest(ServletFileUpload.java:116)
    com.example.model.ServletController.processRequest (ServletController.java:135)
    com.example.model.ServletController.doPost(Servlet Controller.java:1597)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:709)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:585)
    org.apache.catalina.security.SecurityUtil$1.run(Se curityUtil.java:239)
    java.security.AccessController.doPrivileged(Native Method)
    javax.security.auth.Subject.doAsPrivileged(Subject .java:517)
    org.apache.catalina.security.SecurityUtil.execute( SecurityUtil.java:266)
    org.apache.catalina.security.SecurityUtil.doAsPriv ilege(SecurityUtil.java:157)


    note The full stack trace of the root cause is available in the Apache Tomcat/5.0.27 logs.




    The following is my code for picture uploading
    ==================================

    //================================================== =============================
    // Check that we have a file upload request

    boolean isMultipart = FileUpload.isMultipartContent(request);

    if ( isMultipart == true) {

    String fieldname = null;
    String fieldvalue = null;
    String fileName = null;


    DiskFileItemFactory factory = new DiskFileItemFactory();
    // Configure the factory here, if desired.
    ServletFileUpload diskFileUpload = new ServletFileUpload(factory);
    // Configure the uploader here, if desired.


    List fileItemsList = null;
    try {
    // diskFileUpload.setSizeThreshold(40960);
    // diskFileUpload.setSizeMax(81920);

    fileItemsList = diskFileUpload.parseRequest(request);
    } catch (FileUploadException ex) { ex.printStackTrace(); }

    String optionalFileName = "";
    FileItem fileItem = null;
    col_name_coma = "";
    col_value_coma = "";
    name_value = "";

    Iterator it = fileItemsList.iterator();
    while (it.hasNext()){
    FileItem fileItemTemp = (FileItem)it.next();

    // fieldname = fileItemTemp.getFieldName();
    // fieldvalue = fileItemTemp.getString();


    if (fileItemTemp.isFormField()){
    fieldname = fileItemTemp.getFieldName();
    fieldvalue = fileItemTemp.getString();
    if (fieldname.equals("profile_id")) {
    profile_id = fieldvalue;
    col_name_coma = col_name_coma + fieldname +",";
    col_value_coma = col_value_coma + "'"+fieldvalue+"','";
    }
    if (fieldname.equals("Router")) { router = fieldvalue; }
    if (fieldname.equals("DisplayInputForm")) { display_input_form = fieldvalue; }
    if (fieldname.equals("UserType")) { user_type = fieldvalue; }
    if (fieldname.equals("display_option")) {
    display_option = fieldvalue;
    col_name_coma = col_name_coma + fieldname +",";
    col_value_coma = col_value_coma + fieldvalue+"','";
    name_value = name_value + "display_option = "+"'"+display_option+"'";
    }

    if (fileItemTemp.getFieldName().equals("filename")) optionalFileName = fileItemTemp.getString();
    }
    else fileItem = fileItemTemp;

    if (fileItem!=null){ // label-A

    fileName = fileItem.getName();

    // Uploaded File Info
    // Content type: <%= fileItem.getContentType() %><br/>
    // Field name: <%= fileItem.getFieldName() %><br/>
    // File name: <%= fileName %><br/>
    // File size: <%= fileItem.getSize() %><br/><br/>


    /* Save the uploaded file if its size is greater than 0. */
    if (fileItem.getSize() > 0){ // label-B
    if (optionalFileName.trim().equals("")) {
    fileName = (new File(fileName)).getName();
    // File saveTo = new File(fileName);
    fileName = profile_id + "-"+ fileName;
    String picture_no = fileItem.getFieldName();
    col_name_coma = col_name_coma + picture_no +",";
    // col_value_coma = col_value_coma + saveTo +"','";
    // name_value = name_value +" , "+picture_no+" = "+"'"+saveTo+"'";
    col_value_coma = col_value_coma + fileName +"','";
    name_value = name_value +" , "+picture_no+" = "+"'"+fileName+"'";
    } else {
    fileName = profile_id + "-"+ optionalFileName;
    String picture_no = fileItem.getFieldName();
    col_name_coma = col_name_coma + picture_no +",";
    col_value_coma = col_value_coma + fileName +"','";
    name_value = name_value +" , "+picture_no+" = "+"'"+fileName+"'";
    }

    String dirName = "/images/member/";

    File saveTo = new File(dirName + fileName);
    try {
    fileItem.write(saveTo);

    //<b>The uploaded file has been saved successfully.</b>
    }
    catch (Exception e){
    e.printStackTrace() ;
    // request.setAttribute("tran_result","File Not Loaded");
    // request.setAttribute("page_type","any");
    // request.setAttribute("page_name","TransactionResult.jsp");
    //
    // RequestDispatcher view = request.getRequestDispatcher("main_page.jsp");
    // response.setContentType("text/html;charset=UTF-8");
    // view.forward(request, response);
    // return;
    // <b>An error occurred when we tried to save the uploaded file.</b>
    } // end-catch
    save_database(col_name_coma,col_value_coma);
    request.setAttribute("tran_result","N-File uploaded Sucessfully");
    request.setAttribute("page_type","any");
    request.setAttribute("page_name","TransactionResult.jsp");
    RequestDispatcher view = request.getRequestDispatcher("main_page.jsp");
    response.setContentType("text/html;charset=UTF-8");
    view.forward(request, response);
    return;
    } // end-label-B
    } // end-label-A
    } // end-while
    } // end multi-part = true

    //==============================P I C T U R E - UPLOADING ================================================== =

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

  2. #2
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    255
    You need to grant permissions to write a file to the directory "/images/member". Try physically navigating to the directory with the user used by the webapp and create a new file.
    Happiness is good health and a bad memory.

Similar Threads

  1. Replies: 9
    Last Post: 10-08-2008, 07:03 AM
  2. Replies: 3
    Last Post: 05-30-2002, 09:38 AM
  3. Replies: 12
    Last Post: 05-18-2001, 04:05 AM
  4. develpers needed for a revolutionary p2p network client
    By Patrick Lauber in forum Open Source
    Replies: 0
    Last Post: 03-07-2001, 10:52 AM
  5. Comdlg32, select a FILE from FONTS directory!
    By Peter in forum VB Classic
    Replies: 8
    Last Post: 06-16-2000, 11:53 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links