wood burning stoves 2.0*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked 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: 12759
    
    5
PLEASE do not post the same question to multiple forums. It wastes everybodys time and bandwidth. I already responded to this elsewhere.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to write Image on Web Host Directory
 
Similar Threads
Unable To Write Image on Web Host Directory
File Upload using Jsp and servlets...
Image upload problem
problem with file uploading in struts
getting the uploaded filename for downloading