Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

converting contextPath for Linux

Status
Not open for further replies.

jondubya

Technical User
Apr 16, 2005
13
0
0
GB
I'm trying to convert a piece of code that I got working on Windows, but I am unable to convert for Linux. I'm trying to upload an image using <input type="file" name="filename" size="50">

The following is the submitting page(upload.jsp) which attempts to upload the file located from the previous page, written exactly as it is for my Windows server.
--------------------------------------------------------

<%@ page import="java.util.*,java.io.*"%>

<%
String path=request.getParameter("filename");
String newPath="";
int count=0;

if(path!=null)
{
ArrayList arr=new ArrayList();
StringTokenizer st=new StringTokenizer(path,"\\");
while(st.hasMoreTokens())
{
arr.add(count,st.nextToken());
count++;
}
// create ur own path

newPath="C:\\Tomcat5\\webapps\\meetings\\xmasxonference\\upload\\"+arr.get(count-1);
int c;
FileInputStream fis=new FileInputStream(path);
FileOutputStream fos=new FileOutputStream(newPath);
while((c=fis.read())!=-1)
{
fos.write((char)c);
}
}

out.println("Thanks for using");
out.println("<br>");
out.println("<br>");
out.println("1.File1 Uploaded from :: "+path);
out.println("<br>");
out.println("<br>");
out.println("2.Uploaded File1 is Saved in :: "+newPath);
%>
--------------------------------------------
I've tried setting the following

newPath = /home/tomcat/jakarta-tomcat-5.0.18/webapps/meetings/xmas/images/"+arr.get(count-1);

and changing the tokenizer, as well as using
String appPath = application.getRealPath("/");
to get a relative path, which is preferable but with no success.

Any pointers, anyone? TIA dubya
 
Well any path using the file syntax "C:/bla" is not going to work on linux.

What are the errors ?
Where do you actually want to save the file ?
BTW, saving a file to the webapp directory is seriously bad form. What would happen when you wanted to deploy the webapp again ? Much better to save uploaded files in a standard, backed up, data directory.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Hi

I am little confused with the code , If I understood it correctly you are trying to upload a file using <input type="file"/> from client to the server? I think your code will work only if the client and server are sitting on the same machine . More over in the previous page the HTML form should be of type multipart/form-data and you cannot access the patameter uisng request.getParameter("path") if the form of type multipart . I might be wrong as you mentioned it is working in Windows. Any how If you need the context path of the upload.jsp you can get it using

Code:
<%=this.getServletConfig().getServletContext().getRealPath("/")%>

Sedj has some good comments a well.

Cheers
Venu
 
Appart from all posted before, take care when parsing the path, it might not contain \\ at all but /.

Cheers,
Dian
 
Thanks for the pointers, I've taken your advice and am now trying to avoid saving in the application folders. Having got a working page, I've just discovered my problem now is that JSP doesn't support multipart/form-data enctypes! So I can upload an image but I cannot pass regular form data across at the same time so I'm just looking through jakarta-commons as a solution. Thanks to all for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top