hi friends,
I am a novice java programmer.
I am in a learning process in java servlets.
But i struck into a uploading a file into (desired location) server
I will show my code as some one can point out where am i going wrong. Currently im dealing with null value exception and it also says ServletConfig needs to be initialized. Any one help me to get rid of that. Or suggest me a new (standard) method to solve it.
in doPost function
if(request.getParameter("action").toString()!=null){
String action =request.getParameter("action").toString();
if(action.equals("Upload")){
ProjectUtility util =new ProjectUtility ();
util.uploadFile(request, response);
System.out.print(request.getParameter("texte"));
}
}
my ProjectUtility class
public void uploadFile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DiskFileItemFactory fileItemFactory = new DiskFileItemFactory ();
/*
*Set the size threshold, above which content will be stored on disk.
*/
fileItemFactory.setSizeThreshold(1*1024*1024); //1 MB
/*
* Set the temporary directory to store the uploaded files of size above threshold.
*/
File tmpDir=new File("C:\\temp");
fileItemFactory.setRepository(tmpDir);
ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
try {
/*
* Parse the request
*/
List items = uploadHandler.parseRequest(request);
Iterator itr = items.iterator();
while(itr.hasNext()) {
FileItem item = (FileItem) itr.next();
/*
* Handle Form Fields.
*/
if(item.isFormField()) {
} else {
/*
* Write file to the ultimate location.
*/
File file = null;
String destination = getServletContext(). getRealPath("/files/");
file = new File(destination,item.getName());
item.write(file);
}
}
}catch(FileUploadException ex) {
ex.printStackTrace();
} catch(Exception ex) {
ex.printStackTrace();
}
}
and im importing which doesnt cause me any issues
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
my HTML file
<%
out.println("<form name=\"uploadForm\" method=\"POST\" action=\"/Controller\" enctype=\"multipart/form-data\" >");
out.println("<input type='text' name='texte' value=''>");
out.println("<input type='file' name='fileImage'>");
out.println(" <input type=\"submit\" name=\"action\" value=\"Upload\">");
%>
thanks & regards