mercredi 4 février 2015

Can't get path to upload on host. Jsp

Please help me,i was tried to find answer on google for whole week but no luck for me. Nothing work with me at all, and this is my problem, i will really appriciate your help.


i want to upload a picture on my host and i used code like this and it worked fine on localhost but not in my real Hosting. This is my code:



boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
}
else {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;

try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
Iterator iter = items.iterator();



while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
params.put(item.getFieldName(), item.getString());
} else {
try {
String itemName = item.getName();
filename = rdhash.randomHash() + itemName.substring(
itemName.lastIndexOf("\\") + 1);
String realPath = getServletContext().getRealPath("/")
+ "banner\\" + filename;

out.println(realPath);
File savedFile = new File(realPath);
item.write(savedFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}


as i said, it can not work on my real hosting. I was try to find why it doesn't work and i find out that it can not get the upload path, it likely can't get the upload path because the out.println() code show me like this:



null\banner\[my file name].jpg


but when i try on my localhost it is show whole path to my project like:



C:\[myproject folder]\banner\[my file name].jpg


i don't know why it can't get the realpath on my host. Please help me, my mind is blowing now. My hosting server is Tomcat 7 bros.





Aucun commentaire:

Enregistrer un commentaire