2

My code is working fine on local machine. but when I upload it to server it's not working.

Here is my code

html file

<html>
<head>
<form action="fileUpload.jsp" name="upform" enctype="multipart/form-data">
<table width="60%" border="0" cellspacing="1" cellpadding="1" align="center" class="style1">
<tr>
<td align="left"><b>Select a file to upload :</b></td>
</tr>
<tr>
<td align="left">
<input type="file" name="filename" size="50">
</td>
</tr>
<tr>
<td align="left">
<input type="hidden" name="todo" value="upload">
<input type="submit" name="Submit" value="Upload">
<input type="reset" name="Reset" value="Cancel">
</td>
</tr>
</table>
</form>
</body>
</html>

fileUpload.jsp

<%@ page import="java.util.*,java.io.*"%>
<%
String path=request.getParameter("filename");
String newPath="";
int count=0;
try{
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="/home/sumesh/workspace/TaskManager/WebContent/Pages/Files/"+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);
}
}
catch (Exception err){
    out.println(err);
}
}
%>

How can I solve this?

Aba Dov
  • 962
  • 4
  • 12
  • 20
Syamkumar K S
  • 119
  • 3
  • 8
  • Whats the error when you run this code? Does this path is correct for your server machine where you deploy it::`/home/sumesh/workspace/TaskManager/WebContent/Pages/Files/` – Harry Joy Feb 22 '11 at 10:31

1 Answers1

2

First of all, you should not implement this is a JSP, but in a Servlet (or an action of your favorite MVC framework : Stripes, Spring MVC, Struts, etc.) JSPs are meant for presentatino code only, using HTML, the JSTL and custom JSP tags.

To handle file uploads, you should use a dedicated API such as Apache commons FileUpload, because the servlet API doesn't have direct support for multi-part requests. All MVC frameworks I know about also include support for file uploads.

Now for the explanation of why it works on your local machine : when you open the input stream to the path sent as parameter in the request, you open an input stream using the path of the file on the client's machine. Since in this case, the server machine is also the client machine, it works. But as soon as the server is not the client anymore, it doesn't work anymore.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • for my knowledge what you mean by "you open an input stream using the....it does not work any more."? Will you please elaborate it? – Harry Joy Feb 22 '11 at 10:40
  • @Herry Joy: The OP gets the path fromt the request parameter (`String path=request.getParameter("filename");`) and then opens a file input stream with this path (`FileInputStream fis=new FileInputStream(path);`). This code uses the path of the file **on the client's machine** to try reading the uploaded file. When run on the server machine, it can't possibly work. – JB Nizet Feb 22 '11 at 10:45
  • hey... its `Harry` not `Herry`. Read name carefully. – Harry Joy Feb 22 '11 at 11:49
  • @Harry: the OP expected for some reason that the server machine has instant access to client's local disk file system and thus client specific file system paths works "as usual". This is utterly wrong. See also [this answer](http://stackoverflow.com/questions/81180/how-to-get-the-file-path-from-html-input-form-in-firefox-3/3374408#3374408) for technical explanation and [this answer](http://stackoverflow.com/questions/2422468/how-to-upload-files-in-jsp-servlet/2424824#2424824) for the right way (note that OP also forgot POST method while that's required in combination with the given enctype). – BalusC Feb 22 '11 at 14:55
  • thanks JB Nizet, As you said jsp is for presentation purposes.... but in my case jsp is the only option... if you have any sample written jsp please share... – Syamkumar K S Feb 23 '11 at 05:30