• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Please tell the reason for errors in the code. CODE and Errors given

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi i am trying to run following servlet code and getting errors which i have shown after the code. can anybody tell where is the problem?

// Code for FileUploadCommons



package com.jspbook;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
import java.util.*;

public class FileUploadCommons extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

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

out.println("<html>");
out.print("File upload success. <a href=\"/jspbook/files/");
out.print("\">Click here to browse through all uploaded ");
out.println("files.</a><br>");

ServletContext sc = getServletContext();
String path = "c:/files";
org.apache.commons.fileupload.FileUpload fu = new org.apache.commons.fileupload.FileUpload();
fu.setSizeMax(-1);
fu.setRepositoryPath(path);
try {
List l = fu.parseRequest(request);
Iterator i = l.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem)i.next();
// trim out full path info if it is included
String filename = fi.getName();
int slash = filename.lastIndexOf("\\");
if (slash != -1) {
filename = filename.substring(slash + 1);
}
// write the file to the 'files' directory
fi.write(path+"/"+filename);
}
}
catch (Exception e) {
throw new ServletException(e);
}

out.println("</html>");
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
doPost(request, response);
}
}



// I am getting following error messages on Compilation




C:\>javac FileUploadCommons.java
FileUploadCommons.java:27: cannot resolve symbol
symbol : method setRepositoryPath (java.lang.String)
location: class org.apache.commons.fileupload.FileUpload
fu.setRepositoryPath(path);
^
FileUploadCommons.java:40: write(java.io.File) in org.apache.commons.fileupload.
FileItem cannot be applied to (java.lang.String)
fi.write(path+"/"+filename);
^
2 errors
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



1.) class.org.apache.commons.fileupload.FileUpload does not have a setRepositoryPath() method. Look at the API to figure out what the name of the method you're looking for is.

2.) The FileItem.write(java.io.File) can not take a String as an argument.
It needs a File object.
 
This parrot is no more. It has ceased to be. Now it's a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic