Error in file uploading code - java

I am getting following error in jsp page for file upload code:
The method parseRequest(RequestContext) in the type FileUploadBase is not applicable for the arguments (HttpServletRequest)
error in the code:
List<FileItem> items = uploadHandler.parseRequest(request);

The parseRequest(RequestContext ctx) expects RequestContext instance as argument but the argument passed is instance of HttpServletRequest
Use ServletRequestContext to create a RequestContext instance as follows.
List<FileItem> items
= uploadHandler.parseRequest(new ServletRequestContext(request));

I had same problem, then found my imports were wrong: the last one of them was using fileupload from sun, not from commons.fileupload. After I changed them all to commons.fileupload, the error disappeared:
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.servlet.ServletRequestContext;

great answers above, but if you are upgrading to tomcat10 which naming change from javax to jakarta, commons-fileupload as of version 1.4 has not change the naming yet, but you can change to the custom class in tomcat10 instead! (LUCKY ME)
org.apache.commons.fileupload.ProgressListener to org.apache.tomcat.util.http.fileupload.ProgressListener
org.apache.commons.fileupload.servlet.ServletFileUpload to org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload
org.apache.commons.fileupload.disk.DiskFileItemFactory to org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory
org.apache.commons.fileupload.FileItem to org.apache.tomcat.util.http.fileupload.FileItem

Related

Facing a problem with using Natty library for converting string to date [duplicate]

I get the following error despite it's exactly one used in examples:
error: type List does not take parameters
List<String> strings_wat = new ArrayList<String>();
Java is version 1.7 and the class is NOT named ArrayList.
You are likely importing java.awt.List.
You should instead import java.util.List, which is a parameterized type.
It seems like you are importing it from java.awt:
import java.awt.List;
and it looks like you want to use the one from java.util:
import java.util.List;
if you are working on Graphical user interface you have to
import java.awt.List instead of import java.util.List
Other then else if you are working on simple code you have to import java.util.List; instead of import java.awt.List
I am getting solved this problem by renaming the class name or file name as in Notepad++.
You Should not take the class names as ArrayList or List.
And you also need to add the following package
import java.util.*;
My Previous class name is List I changed it into the Names.so You have to change the class name.

error: type List does not take parameters

I get the following error despite it's exactly one used in examples:
error: type List does not take parameters
List<String> strings_wat = new ArrayList<String>();
Java is version 1.7 and the class is NOT named ArrayList.
You are likely importing java.awt.List.
You should instead import java.util.List, which is a parameterized type.
It seems like you are importing it from java.awt:
import java.awt.List;
and it looks like you want to use the one from java.util:
import java.util.List;
if you are working on Graphical user interface you have to
import java.awt.List instead of import java.util.List
Other then else if you are working on simple code you have to import java.util.List; instead of import java.awt.List
I am getting solved this problem by renaming the class name or file name as in Notepad++.
You Should not take the class names as ArrayList or List.
And you also need to add the following package
import java.util.*;
My Previous class name is List I changed it into the Names.so You have to change the class name.

Issues with HttpClient 4.3.1 creating instance of HttpClient

I am trying to convert Http upload to use the new HttpClient 4.3.1 classes. I'm new to Java. Everything I find online uses deprecated classes (i.e. HttpClient client = new DefaultHttpClient() or an even older method for creating an instance of HttpClient. Forgive all the extra libraries below, some will be needed in the rest of my project.
I've tried umpteen different ways to create the instance, what I have below is the method I see used in org.appache documenation for 4.3.1.
Unfortunately, I'm getting an error indicating that HttpClientBuilder is not visible. I'm not even sure what not visible means...the library has been imported. Can anyone point me in the right direction for creating an HttpClient instance.
package newHttpApiUpload;
import org.apache.http.client.HttpClient;
import org.apache.http.HttpConnection;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.AbstractHttpEntity;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class Api {
protected static final HttpClientBuilder client = new HttpClientBuilder();
}
The constructor HttpClientBuilder() is protected (not public) so you cannot call it from your code. That is what not visible means.
You invoke the builder using a static constructor method, as such:
HttpClientBuilder.create();
Or, to quickly create a client (which is the whole point)
HttpClient client = HttpClientBuilder.create()
.setUserAgent("MyAgent")
.setMaxConnPerRoute(4)
.build()
You need to use static factory methods from HttpClients.
You can use them to obtain preconfigured instances of HttpClient, or use HttpClients.custom() to apply your custom configuration using HttpClientBuilder.

Is it possible to extend a Java class with one having the same name

I'm working with a ridiculously large codebase and need to move a file from one package to another. However, I do not have the whole codebase locally, so I am not sure if I have found and updated every reference to the original file. In order to ensure that I don't break anything, I would like to leave the original file, and simply have it extend the new file that I created. Ideally, they'd both have the exact same name. Overtime, I plan to deprecate and remove the old file, but for now this seems like the most robust solution. However, I cannot figure out how to get it to work in Java.
Here is the new class:
package myproject.util.http;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public abstract class MyServlet extends HttpServlet
{
public void service (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// implementation of service
}
}
Here is the old class that extends the new:
package myproject.util.net;
import myproject.util.http.MyServlet;
public abstract class MyServlet extends myproject.util.http.MyServlet
{
// this class was deprecated, use myproject.util.http.MyServlet instead
}
Unfortunately, I get the error:
MyServlet is already defined in this compilation unit
Is this possible, or am I going to have to come up with a new name for the parent class.
You can do it but you need to remove the import statement.
Otherwise the compilation unit will import all the classes declared in your package to be used without the fully qualified name: this means that you wouldn't be able to distinguish between the two MyServlet, this is why it is illegal.
Just remove import and have fully qualified name myproject.util.http.MyServlet
All you need to do is remove the import statement and all will be well.

Inout and Out parameters in WebServices

I'm using Inout and Out parameters in the ServiceEndPointInterface(SEI).
Here is the method signature.
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.WebParam.Mode;
import javax.jws.Oneway;
import javax.xml.rpc.holders.*;
#WebMethod
#Oneway
public #WebResult void TestDomainCls( #WebParam (mode=Mode.INOUT) IntegerWrapperHolder inpuInt );
And I have implemented this method in the EJBBean. And I have exposed this EJBBean as a webservice using annotation.
While deploying this EAR in JBOSS 5.,it's throwing the error like
Caused by: java.lang.IllegalStateException:
Cannot synchronize to any of these methods:
public abstract java.lang.String MURCOMP.MURCOMP_SEI.serv_20_search1(java.lang.String,javax.xml.rpc.holders.StringHolder)
OperationMetaData:
qname={http://MURCOMP/}serv_20_search1
javaName=serv_20_search1
style=rpc/literal
oneWay=false
soapAction=
ParameterMetaData:
xmlName=arg0
partName=arg0
xmlType={http://www.w3.org/2001/XMLSchema}string
javaType=java.lang.String
mode=IN
inHeader=false
index=0
ParameterMetaData:
xmlName=arg1
partName=arg1
xmlType={http://www.w3.org/2001/XMLSchema}anyType
javaType=java.lang.Object
mode=OUT
inHeader=false
index=1
ReturnMetaData:
xmlName=return
partName=return
xmlType={http://www.w3.org/2001/XMLSchema}string
javaType=java.lang.String
mode=OUT
inHeader=false
index=-1
at org.jboss.ws.metadata.umdm.OperationMetaData.eagerInitialize(OperationMetaData.java:491)
at org.jboss.ws.metadata.umdm.EndpointMetaData.eagerInitializeOperations(EndpointMetaData.java:559)
at org.jboss.ws.metadata.umdm.EndpointMetaData.initializeInternal(EndpointMetaData.java:543)
at org.jboss.ws.metadata.umdm.EndpointMetaData.eagerInitialize(EndpointMetaData.java:533)
at org.jboss.ws.metadata.umdm.ServiceMetaData.eagerInitialize(ServiceMetaData.java:433)
at org.jboss.ws.metadata.umdm.UnifiedMetaData.eagerInitialize(UnifiedMetaData.java:194)
at org.jboss.wsf.stack.jbws.EagerInitializeDeploymentAspect.start(EagerInitializeDeploymentAspect.java:48)
at org.jboss.wsf.framework.deployment.DeploymentAspectManagerImpl.deploy(DeploymentAspectManagerImpl.java:129)
at org.jboss.wsf.container.jboss50.deployer.ArchiveDeployerHook.deploy(ArchiveDeployerHook.java:76)
at org.jboss.wsf.container.jboss50.deployer.AbstractWebServiceDeployer.internalDeploy(AbstractWebServiceDeployer.java:60)
at org.jboss.wsf.container.jboss50.deployer.WebServiceDeployerEJB.internalDeploy(WebServiceDeployerEJB.java:113)
at org.jboss.deployers.spi.deployer.helpers.AbstractRealDeployer.deploy(AbstractRealDeployer.java:50)
at org.jboss.deployers.plugins.deployers.DeployerWrapper.deploy(DeployerWrapper.java:171)
... 30 more
17:40:12,483 ERROR [ProfileServiceBootstrap] Failed to load profile: Summary of incomplete deployments (SEE PREVIOUS ERRORS FOR DETAILS):
This error is coming only if I'm using inout or out parameters in my method.
Can anyone suggest me.,where I'm going wrong or Is there anything missing with respect to INOUT and OUT parameters in Web Services
enter code here
I have got the solution.,I have used javax.xml.ws.Holder instead of the javax.xml.rpc.holders.
The code is as follows
#WebMethod
public #WebResult String testINout(#WebParam Holder holder);
But I didnt got the actual solution, why if I use javax.xml.rpc.holders such exception has occured. Anyway the alternate way I found to work on..

Categories

Resources