wood burning stoves 2.0*
The moose likes JSP and the fly likes ServletFileUpload error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "ServletFileUpload error" Watch "ServletFileUpload error" New topic
Author

ServletFileUpload error

neelesh kumar
Greenhorn

Joined: Oct 03, 2013
Posts: 29
I am trying to upload file using jsp.....used the servletfileupload class by importing it as follows :
<%@ page import="commons.fileupload.servlet.ServletFileUpload"%>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.*"%>


but i am getting this error on running it :
Only a type can be imported. commons.fileupload.servlet.ServletFileUpload resolves to a package
Only a type can be imported. org.apache.commons.fileupload.disk.DiskFileItemFactory resolves to a package

What to do???
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
The error says you are not importing a specific class and just importing a package. Either import a specific class or .* for all classes in the package.

Also, don't use a JSP to receive fileupload requests. Use a servlet instead and only use JSPs for presentation.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41631
    
  55
Also, don't use a JSP to receive fileupload requests. Use a servlet instead and only use JSPs for presentation.


Nothing that has anything to do with I/O (including, but not limited to, file upload, file system access, DB access etc.) should ever be done in a JSP. In fact, it's recommended that you don't ever use Java in a JSP - just EL and JSTL for generating views.

Also note that you got the package name of the ServletFileUpload class wrong.


Ping & DNS - my free Android networking tools app
neelesh kumar
Greenhorn

Joined: Oct 03, 2013
Posts: 29
using .* did it...but i am having new errors now.. the complete code of my program is :
String uploadPath = "C:/UploadedFiles/" + (String)session.getAttribute("username") + "/decript"+"/";

boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
} else {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if (item.isFormField()) {
} else {
try {
String itemName = item.getName();
File savedFile = new File(uploadPath + itemName);
item.write(savedFile);


} catch (Exception e) {
e.printStackTrace();
}
}
}
//chanchal
// String uploadPath = "C:/UploadedFiles/" + (String)session.getAttribute("username") + "/decript/";
//out.println(uploadPath);
File f = new File(uploadPath);
File[] files = f.listFiles();
for(int i=0;i<files.length;i++){
FileInputStream in = new FileInputStream(files[i]);
File eFile = new File("C:/UploadedFiles/" + (String)session.getAttribute("username") +"/"+files[i].getName());
FileOutputStream outS = null;
outS = new FileOutputStream(eFile);
Crypto.encrypt(in, outS);
try{
outS.close();
in.close();
files[i].delete();
}catch(Exception e){
e.printStackTrace();
}




I am getting the following errors now about class not being resolved :

An error occurred at line: 20 in the jsp file: /hcss_new/build/web/upload_file_multipale.jsp
ServletFileUpload cannot be resolved
17: <%
18: String uploadPath = "C:/UploadedFiles/" + (String)session.getAttribute("username") + "/decript"+"/";
19:
20: boolean isMultipart = ServletFileUpload.isMultipartContent(request);
21: if (!isMultipart) {
22: } else {
23: FileItemFactory factory = new DiskFileItemFactory();


An error occurred at line: 23 in the jsp file: /hcss_new/build/web/upload_file_multipale.jsp
FileItemFactory cannot be resolved to a type
20: boolean isMultipart = ServletFileUpload.isMultipartContent(request);
21: if (!isMultipart) {
22: } else {
23: FileItemFactory factory = new DiskFileItemFactory();
24: ServletFileUpload upload = new ServletFileUpload(factory);
25: List items = null;
26: try {
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41631
    
  55
That's another instance of this issue:
E Armitage wrote:The error says you are not importing a specific class and just importing a package. Either import a specific class or .* for all classes in the package.
neelesh kumar
Greenhorn

Joined: Oct 03, 2013
Posts: 29
how can i overcome this? I have tried everything
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
You have likely created your own package called commons.fileupload.servlet.ServletFileUpload? Or maybe you have extracted Apache's fileupload jar into a new abbreviated package? Don't do any of that. Name your packages according to your organization's name.
The correct import for Apache's ServletFileUpload class is org.apache.commons.fileupload.servlet.ServletFileUpload with the jar file for it on your classpath.
neelesh kumar
Greenhorn

Joined: Oct 03, 2013
Posts: 29
I imported org.apache.commons.fileupload.servlet.ServletFileUpload.* itself....nd i placed the commons-fileupload and commons-io jar files in the web-inf/lib...its still not working
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41631
    
  55
What have you tried? Post all the import statements you currently have. The error message says you're missing "FileItemFactory" - tell us which of the import statements you're going to post covers that.

You didn't reply to my earlier post, but have you fixed this:
Also note that you got the package name of the ServletFileUpload class wrong.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41631
    
  55
I imported org.apache.commons.fileupload.servlet.ServletFileUpload.* itself

That's not the right syntax if you want to import the ServletFileUpload class.
neelesh kumar
Greenhorn

Joined: Oct 03, 2013
Posts: 29
Ulf Dittmer wrote:
I imported org.apache.commons.fileupload.servlet.ServletFileUpload.* itself

That's not the right syntax if you want to import the ServletFileUpload class.


i mean i did it this way :
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload.*"%>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41631
    
  55
The problem is the ".*" at the end - that is for importing packages, not importing classes. It's correct for "org.apache.commons.fileupload.*", because that is a package, but not for the first two, which are classes.

(There are certain circumstances where you would have ".*" at the end of a class name, bot those don't apply here; let's forget those exist for the moment.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ServletFileUpload error