Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
    Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Unable to write Image on Web Host Directory  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Report post to moderator
PLEASE do not post the same question to multiple forums. It wastes everybodys time and bandwidth. I already responded to this elsewhere.
Bill
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!