0

i am new with servlet programming, i need to develop a .WAR API using servlet which will upload only xlsx files to a specific path in server (this API will be using by WebFocus (a programming language for reporting))

the specific path will be in a hidden filed of html <form>

here is my HTML

<html>  
<body>  
<form action="UploadServlet" method="Post" enctype="multipart/form-data">  
Select File:<input type="file" name="fname"/><br/>

  <input type="hidden" name="path" value="G:/newFolder" />
<input type="submit" value="upload"/>  

</form>  
</body>  
</html>

now what should write inside my UploadServlet.java file in order to upload xlsx files into path defined in path hidden filed

currently in my UploadServlet.java i used MultipartRequest and it uploads the file to destination correctly but i want the distenation to be the value of path hidden field of the <form>

import java.io.*;  
import javax.servlet.ServletException;  
import javax.servlet.http.*;  
import com.oreilly.servlet.MultipartRequest;  

public class UploadServlet extends HttpServlet {  

public void doPost(HttpServletRequest request, HttpServletResponse response)  
    throws ServletException, IOException {  

response.setContentType("text/html");  
PrintWriter out = response.getWriter();  

MultipartRequest m=new MultipartRequest(request,"g:/newFolder");  
out.print("successfully uploaded");  
}  
} 

any help would be much appreciated.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

1 Answers1

0

Simply replace the path in your java code with the value of the submitted variable:

String uploadPath = request.getParameter("path");
MultipartRequest m = new MultipartRequest(request, uploadPath);

Now your MultipartRequest gets initialized with the value from the hidden field.

Please note that this constructor of MultipartRequest limits the upload size to 1 megabyte.

f1sh
  • 11,489
  • 3
  • 25
  • 51