wood burning stoves*
The moose likes Java in General and the fly likes Unable To Write Image on Web Host Directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Unable To Write Image on Web Host Directory" Watch "Unable To Write Image on Web Host Directory" New topic
Author

Unable To Write Image on Web Host Directory

Mohammad Ali
Greenhorn

Joined: Jan 03, 2006
Posts: 11
Please I need help, i am trying to write an image file to the web server directory but i am not able to write image file on the web server directory. I am using Apachee Fileupload to write image file on server directory. My java code works fine when i run the application locally on my laptop but once i upload the application on the server it does not let me write the image to the directory on web host server. I checked the permissions on my server directory they are all good rwx on all user group and public. please give me advise what i am doing wrong.

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+"','";

}

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);
save_database(col_name_coma,col_value_coma);
request.setAttribute("tran_result","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;
//<b>The uploaded file has been saved successfully.</b>
}
catch (Exception e){

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
} // end-label-B
} // end-label-A
} // end-while
} // end multi-part = tru
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5

You appear to be throwing away the exception that probably contains the reason. Why not attach it to the request and display it in main_page.jsp?
Bill
Mohammad Ali
Greenhorn

Joined: Jan 03, 2006
Posts: 11
Hello, Bill i removed the code from CATCH but still it is not able to write image file on my server directory, it is throwing the following error , please guide me what i am doing wrong, the same code is working fine and loading images when i run the app on my laptop locally

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(AccessController.java:427)
java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
java.lang.SecurityManager.checkWrite(SecurityManager.java:962)
java.io.FileOutputStream.<init>(FileOutputStream.java:169)
java.io.FileOutputStream.<init>(FileOutputStream.java:131)
org.apache.commons.io.output.DeferredFileOutputStream.thresholdReached(DeferredFileOutputStream.java:122)
org.apache.commons.io.output.ThresholdingOutputStream.checkThreshold(ThresholdingOutputStream.java:221)
org.apache.commons.io.output.ThresholdingOutputStream.write(ThresholdingOutputStream.java:128)
org.apache.commons.fileupload.MultipartStream.readBodyData(MultipartStream.java:520)
org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:371)
org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:116)
com.example.model.ServletController.processRequest(ServletController.java:135)
com.example.model.ServletController.doPost(ServletController.java:1597)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:585)
org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.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.doAsPrivilege(SecurityUtil.java:157)


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

Joined: Apr 02, 2004
Posts: 884
Do you have the access rights to write to that directory?


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5

The interesting thing here is that the directory is NOT the one you attempted to define in the code "/images/member". Is that /temp/ from Apache Fileupload?

Your permissions on the server are not whats important - whats important is the permissions Tomcat has.

Bill
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Originally posted by William Brogden:
Your permissions on the server are not whats important - whats important is the permissions Tomcat has.


Do you mean the permissions of the Tomcat user?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Do you mean the permissions of the Tomcat user?

I'm not sure what "Tomcat user" means in this context.
Tomcat runs with permissions established when it starts, so if you start Tomcat - it runs with your permissions. If Tomcat is started as a service it gets particular permissions when the server creates it - I suppose the way these are established depends on the server operating system - not something I am familiar with.

Bill
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Just to clarify a bit, the "permissions" involved here are not the permissions enforced by the operating system, they are the permissions enforced by the Java security manager built into Tomcat.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41614
    
  55
Paul makes an important point - Tomcat seems to be running with a security manager, and that disallows file I/O to the /web/tomcat/temp directory. It does not matter whether the user under which Tomcat runs has permissions on that directory (although that is an additional requirement).
You need to configure FileUpload so that it stores the file in a directory in your web app (maybe somewhere in WEB-INF, so that they can't be downloaded). Or check with the sys admin where your web app is allowed to create files.


Ping & DNS - my free Android networking tools app
Mohammad Ali
Greenhorn

Joined: Jan 03, 2006
Posts: 11
Please guide me, i am stuck with this picture uploading problem, I rent a space on a shared tomcat server from a serevice provider, i am not allowed to customise tomcat in any way. My application works perfect when i run the same java code on my local laptop machinem , i do not understand why tomcat write to a windows directory on my local machine but unable to write on the server directory. I do not want to store images in database bcz of loading/unloading images from table takes to much time and is not a good smart solution. I appreciate all help from this forum. I wait for your advise.... Thanks
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
As Paul and Ulf pointed out, the nature of that exception means that Tomcat is running with security manager on - the only sensible way to run a public server. Security managers allow very fine grain control over what individual servlets are allowed to do. You need to talk to the server administrator.

Read more about the security manager in your own Tomcat installation at:
/tomcat-docs/security-manager-howto.html

My suggestion about the Tomcat user permissions was wrong, if that had been the problem, I think you would have gotten some kind of IOException.

Bill
 
wood burning stoves
 
subject: Unable To Write Image on Web Host Directory