FileUpload 在 HttpRequest 过程中

huangapple go评论70阅读模式
英文:

FileUpload during HttpRequest

问题

I'm trying to do an HTTP GET taking a parameter from the URL and then search into 2 files and send back the information to the user.
I've tried the Java code in a Main Test without any server or request, and it works without any problem, and also the taking of parameters from the URL works.

Error Message:

HTTP Status 500 – Internal Server Error
Type Exception Report

Message org.glassfish.jersey.server.ContainerException: java.io.FileNotFoundException: \AutoCompilersz\src\net\codejava\ws\italy_cap.json (Impossibile trovare il percorso specificato)

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.io.FileNotFoundException: \AutoCompilersz\src\net\codejava\ws\italy_cap.json (Impossibile trovare il percorso specificato)
...

Java File:

package net.codejava.ws;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;

@Path("/form")
public class ComuniResource {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public ArrayList<Cities> comuni(@QueryParam("cap") String cap) throws FileNotFoundException, IOException {
        // ... (rest of the Java code)
    }
}

Pom.xml:

<!-- The content of your POM.xml file -->

Web.xml:

<!-- The content of your Web.xml file -->

Hope this helps with your problem.

英文:

I'm trying to do an HTTP GET taking a parameter from the URL and then search into 2 file and send back the informations to the user.
I've Tried the Java code into a Main Test without any server or request, and it works without any problem and also the taking of parameters from the URL works.
There are other classes made because the file is a Json Array of Objects.

Error Message

HTTP Status 500 – Internal Server Error
Type Exception Report

Message org.glassfish.jersey.server.ContainerException: java.io.FileNotFoundException: \AutoCompilersz\src\net\codejava\ws\italy_cap.json (Impossibile trovare il percorso specificato)

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.io.FileNotFoundException: \AutoCompilersz\src\net\codejava\ws\italy_cap.json (Impossibile trovare il percorso specificato)
	org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:408)
	org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)
	org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:365)
	org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:318)
	org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Java File

package net.codejava.ws;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;

@Path(&quot;/form&quot;)
public class ComuniResource {
	@GET
	@Produces(MediaType.TEXT_PLAIN)
	public ArrayList&lt;Cities&gt; comuni(@QueryParam(&quot;cap&quot;) String cap) throws FileNotFoundException, IOException {
		Foglio1 p=new Foglio1();
		ArrayList &lt;String&gt; istat =  new ArrayList&lt;String&gt;();
		try (JsonReader reader=new JsonReader(new FileReader(&quot;/AutoCompilersz/src/net/codejava/ws/italy_cap.json&quot;))) {
			Gson g = new Gson();
			p=g.fromJson(reader, Foglio1.class);
			//	System.out.println(p);
			//System.out.println(p.Foglio1.length);
			for(int i=0;i&lt;p.Foglio1.length;i++) {
				if(p.Foglio1[i].cap.contentEquals(cap)) {
					//System.out.println(p.Foglio1[i]);
					istat.add(p.Foglio1[i].istat.toString());
				}
			}
		}
		Foglio_1 pp=new Foglio_1();
		Result result=new Result();
		try(JsonReader readerz=new JsonReader(new FileReader(&quot;/AutoCompilersz/src/net/codejava/ws/italy_cities.json&quot;))){
			Gson g=new Gson();
			pp=g.fromJson(readerz, Foglio_1.class);
			//System.out.println(pp);
			//System.out.println(pp.Foglio1.length);
			for(int i=0;i&lt;pp.Foglio1.length;i++) {
				for(int x=0;x&lt;istat.size();x++) {
					if(pp.Foglio1[i].istat.contentEquals(istat.get(x))) {
						result.result.add(pp.Foglio1[i]);//pp.Foglio1[i]
					}
				}
			}
		}
		return result.result;
	}
}

Pom.xml

&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;groupId&gt;net.codejava.ws&lt;/groupId&gt;
&lt;artifactId&gt;AutoCompilersz&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;packaging&gt;war&lt;/packaging&gt;
&lt;dependencies&gt;
&lt;!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.google.code.gson&lt;/groupId&gt;
&lt;artifactId&gt;gson&lt;/artifactId&gt;
&lt;version&gt;2.8.5&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.glassfish.jersey.containers&lt;/groupId&gt;
&lt;artifactId&gt;jersey-container-servlet&lt;/artifactId&gt;
&lt;version&gt;2.29&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.glassfish.jersey.core&lt;/groupId&gt;
&lt;artifactId&gt;jersey-server&lt;/artifactId&gt;
&lt;version&gt;2.29&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.glassfish.jersey.inject&lt;/groupId&gt;
&lt;artifactId&gt;jersey-hk2&lt;/artifactId&gt;
&lt;version&gt;2.29&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.googlecode.json-simple&lt;/groupId&gt;
&lt;artifactId&gt;json-simple&lt;/artifactId&gt;
&lt;version&gt;1.1.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.javassist&lt;/groupId&gt;
&lt;artifactId&gt;javassist&lt;/artifactId&gt;
&lt;version&gt;3.26.0-GA&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.glassfish.jersey.media&lt;/groupId&gt;
&lt;artifactId&gt;jersey-media-json-jackson&lt;/artifactId&gt;
&lt;version&gt;2.29.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-annotations-api&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-catalina-ant&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-catalina-ha&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-ssi&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-storeconfig&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-tribes&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-catalina&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.eclipse.jdt&lt;/groupId&gt;
&lt;artifactId&gt;ecj&lt;/artifactId&gt;
&lt;version&gt;3.21.0&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-el-api&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-jasper-el&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-jasper&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-jaspic-api&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-jsp-api&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-servlet-api&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-api&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-coyote&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-dbcp&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-i18n-cs&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-i18n-de&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-i18n-es&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-i18n-fr&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-i18n-ja&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-i18n-ko&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-i18n-pt-BR&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-i18n-ru&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-i18n-zh-CN&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-jdbc&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-jni&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-util-scan&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-util&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-websocket&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-websocket-api&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;sourceDirectory&gt;src&lt;/sourceDirectory&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
&lt;version&gt;3.8.0&lt;/version&gt;
&lt;configuration&gt;
&lt;release&gt;13&lt;/release&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
&lt;version&gt;3.2.3&lt;/version&gt;
&lt;configuration&gt;
&lt;warSourceDirectory&gt;WebContent&lt;/warSourceDirectory&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

Web.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;web-app xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns=&quot;http://xmlns.jcp.org/xml/ns/javaee&quot; xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd&quot; id=&quot;WebApp_ID&quot; version=&quot;4.0&quot;&gt;
&lt;display-name&gt;AutoCompilersz&lt;/display-name&gt;
&lt;welcome-file-list&gt;
&lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
&lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;
&lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
&lt;welcome-file&gt;default.html&lt;/welcome-file&gt;
&lt;welcome-file&gt;default.htm&lt;/welcome-file&gt;
&lt;welcome-file&gt;default.jsp&lt;/welcome-file&gt;
&lt;/welcome-file-list&gt;
&lt;servlet&gt;
&lt;servlet-name&gt;Jersey REST Service&lt;/servlet-name&gt;
&lt;servlet-class&gt;org.glassfish.jersey.servlet.ServletContainer&lt;/servlet-class&gt;
&lt;init-param&gt;
[enter image description here][1]&lt;param-name&gt;jersey.config.server.provider.packages&lt;/param-name&gt;
&lt;param-value&gt;net.codejava.ws&lt;/param-value&gt;
&lt;/init-param&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
&lt;servlet-name&gt;Jersey REST Service&lt;/servlet-name&gt;
&lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
&lt;/web-app&gt;

Hope can you help me with my problem.
Thank you FileUpload 在 HttpRequest 过程中

The photo of My Project Explorer
[1]: https://i.stack.imgur.com/4b2no.png

答案1

得分: 0

我是这样解决的:

try (InputStream is = getClass().getResourceAsStream("/resource/italy_cap.json")) {
    InputStreamReader isr = new InputStreamReader(is);
    JsonReader reader = new JsonReader(isr);
    Gson g = new Gson();
    p = g.fromJson(reader, Foglio1.class);
}
英文:

I solved it like this:

try (InputStream is = getClass().getResourceAsStream(&quot;/resource/italy_cap.json&quot;)) {
InputStreamReader isr = new InputStreamReader(is);
JsonReader reader=new JsonReader(isr);
Gson g = new Gson();
p=g.fromJson(reader, Foglio1.class);

huangapple
  • 本文由 发表于 2020年8月6日 18:58:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63282140.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定