jax-rs GlassFish4上传文件

时间:2015-06-29 15:27:46

标签: eclipse file-upload glassfish jax-rs

美好的一天

我使用EclipseEE Luna,Win7 32bit,Java 8_45和GlassFish 4.1作为AS。我创建了没有Maven的标准动态项目。我想使用Web服务jax-rs将选定的文件上传到定义的文件夹。我使用了(http://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/)的例子。当我想上传(部署)时,Eclipse中的GlassFish显示错误,项目将不会加载到AS。我真的不知道我有什么不好。我做坏事了吗?还是我忘记了什么?

cannot Deploy Java.REST02.GlassFish4.HtmlForm
deploy is failing=Application with name [Java.REST02.GlassFish4.HtmlForm] is not deployed

申请:

package server;
import java.util.*;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import service3.fileupload.*;
@ApplicationPath("server")
public class Server extends Application {
    private final Set<Class<?>> classes;
    public Server() {
        HashSet<Class<?>> c = new HashSet<>();
        c.add(RestService3.class);
        classes = Collections.unmodifiableSet(c);   }
    public Set<Class<?>> getClasses() {
        return classes; }
}

RestService:

package service3.fileupload;
import java.io.*;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;  
import org.glassfish.jersey.media.multipart.FormDataParam;  
@Path("service3")
public class RestService3 {
    @GET
    @Path("test3")
    public String testMetodaString(){
        System.out.println("Vypis - 3");
        return "TEST-vypis3";   }
    //      DO NOT WORK - BY LOADING TO AS IT CRASH
    @POST
    @Path("upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile( @FormDataParam("file") InputStream uploadedInputStream,
                                @FormDataParam("file") FormDataContentDisposition fileDetail) {
        String uploadedFileLocation = "D:\\Download\\" + fileDetail.getFileName();
        writeToFile(uploadedInputStream, uploadedFileLocation);
        String output = "Soubor uložen : " + uploadedFileLocation;
        return Response.status(200).entity(output).build(); 
    }
    private void writeToFile(InputStream uploadedInputStream,String uploadedFileLocation) {     // save uploaded file to new location
        try {
            OutputStream out = new FileOutputStream(new File(uploadedFileLocation));
            int read = 0;
            byte[] bytes = new byte[1024];
            out = new FileOutputStream(new File(uploadedFileLocation));
            while ((read = uploadedInputStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);  }
            out.flush();
            out.close();    }
        catch (IOException e) {
            e.printStackTrace();    }   }
}

HTML PAGE:

<html>
<head>
<meta charset="UTF-8">
<title>WebPage test</title>
</head>
<body>
<h1>Zde test upload souboru pres jax-rs :</h1><br>
<form action="/server/service3/upload" method="post" enctype="multipart/form-data">
    <p>
    Select a file : <input type="file" name="file" size="45""/></p>
    <input type="submit" value="Nahraj Soubor" /></form>
</body>
</html>

我只使用GlassFish系统库:

C:\Program Files\JavaEE\glassfish\modules\bean-validator.jar
C:\Program Files\JavaEE\glassfish\modules\cdi-api.jar
C:\Program Files\JavaEE\glassfish\modules\endorsed\javax.annotation-api.jar
C:\Program Files\JavaEE\glassfish\modules\endorsed\jaxb-api.jar
C:\Program Files\JavaEE\glassfish\modules\endorsed\webservices-api-osgi.jar
C:\Program Files\JavaEE\glassfish\modules\glassfish-api.jar
C:\Program Files\JavaEE\glassfish\modules\ha-api.jar
C:\Program Files\JavaEE\glassfish\modules\javax.batch-api.jar
C:\Program Files\JavaEE\glassfish\modules\javax.ejb-api.jar
C:\Program Files\JavaEE\glassfish\modules\javax.el.jar
C:\Program Files\JavaEE\glassfish\modules\javax.enterprise.concurrent-api.jar
C:\Program Files\JavaEE\glassfish\modules\javax.enterprise.concurrent.jar
C:\Program Files\JavaEE\glassfish\modules\javax.enterprise.deploy-api.jar
C:\Program Files\JavaEE\glassfish\modules\javax.faces.jar
C:\Program Files\JavaEE\glassfish\modules\javax.inject.jar
C:\Program Files\JavaEE\glassfish\modules\javax.interceptor-api.jar
C:\Program Files\JavaEE\glassfish\modules\javax.jms-api.jar
C:\Program Files\JavaEE\glassfish\modules\javax.json.jar
C:\Program Files\JavaEE\glassfish\modules\javax.mail.jar
C:\Program Files\JavaEE\glassfish\modules\javax.management.j2ee-api.jar
C:\Program Files\JavaEE\glassfish\modules\javax.persistence.jar
C:\Program Files\JavaEE\glassfish\modules\javax.resource-api.jar
C:\Program Files\JavaEE\glassfish\modules\javax.security.auth.message-api.jar
C:\Program Files\JavaEE\glassfish\modules\javax.security.jacc-api.jar
C:\Program Files\JavaEE\glassfish\modules\javax.servlet-api.jar
C:\Program Files\JavaEE\glassfish\modules\javax.servlet.jsp-api.jar
C:\Program Files\JavaEE\glassfish\modules\javax.servlet.jsp.jar
C:\Program Files\JavaEE\glassfish\modules\javax.servlet.jsp.jstl-api.jar
C:\Program Files\JavaEE\glassfish\modules\javax.servlet.jsp.jstl.jar
C:\Program Files\JavaEE\glassfish\modules\javax.transaction-api.jar
C:\Program Files\JavaEE\glassfish\modules\javax.websocket-api.jar
C:\Program Files\JavaEE\glassfish\modules\javax.ws.rs-api.jar
C:\Program Files\JavaEE\glassfish\modules\javax.xml.registry-api.jar
C:\Program Files\JavaEE\glassfish\modules\javax.xml.rpc-api.jar
C:\Program Files\JavaEE\glassfish\modules\jaxb-extra-osgi.jar
C:\Program Files\JavaEE\glassfish\modules\jaxb-osgi.jar
C:\Program Files\JavaEE\glassfish\modules\jersey-bean-validation.jar
C:\Program Files\JavaEE\glassfish\modules\jersey-client.jar
C:\Program Files\JavaEE\glassfish\modules\jersey-common.jar
C:\Program Files\JavaEE\glassfish\modules\jersey-container-grizzly2-http.jar
C:\Program Files\JavaEE\glassfish\modules\jersey-container-servlet-core.jar
C:\Program Files\JavaEE\glassfish\modules\jersey-container-servlet.jar
C:\Program Files\JavaEE\glassfish\modules\jersey-entity-filtering.jar
C:\Program Files\JavaEE\glassfish\modules\jersey-gf-cdi.jar
C:\Program Files\JavaEE\glassfish\modules\jersey-gf-ejb.jar
C:\Program Files\JavaEE\glassfish\modules\jersey-guava.jar
C:\Program Files\JavaEE\glassfish\modules\jersey-media-json-jackson.jar
C:\Program Files\JavaEE\glassfish\modules\jersey-media-json-jettison.jar
C:\Program Files\JavaEE\glassfish\modules\jersey-media-json-processing.jar
C:\Program Files\JavaEE\glassfish\modules\jersey-media-moxy.jar
C:\Program Files\JavaEE\glassfish\modules\jersey-media-multipart.jar
C:\Program Files\JavaEE\glassfish\modules\jersey-media-sse.jar
C:\Program Files\JavaEE\glassfish\modules\jersey-mvc-connector.jar
C:\Program Files\JavaEE\glassfish\modules\jersey-mvc-jsp.jar
C:\Program Files\JavaEE\glassfish\modules\jersey-mvc.jar
C:\Program Files\JavaEE\glassfish\modules\jersey-server.jar
C:\Program Files\JavaEE\glassfish\modules\org.eclipse.persistence.antlr.jar
C:\Program Files\JavaEE\glassfish\modules\org.eclipse.persistence.asm.jar
C:\Program Files\JavaEE\glassfish\modules\org.eclipse.persistence.core.jar
C:\Program Files\JavaEE\glassfish\modules\org.eclipse.persistence.dbws.jar
C:\Program Files\JavaEE\glassfish\modules\org.eclipse.persistence.jpa.jar
C:\Program Files\JavaEE\glassfish\modules\org.eclipse.persistence.jpa.jpql.jar
C:\Program Files\JavaEE\glassfish\modules\org.eclipse.persistence.jpa.modelgen.processor.jar
C:\Program Files\JavaEE\glassfish\modules\org.eclipse.persistence.moxy.jar
C:\Program Files\JavaEE\glassfish\modules\org.eclipse.persistence.oracle.jar
C:\Program Files\JavaEE\glassfish\modules\webservices-connector.jar
C:\Program Files\JavaEE\glassfish\modules\webservices-extra-jdk-packages.jar
C:\Program Files\JavaEE\glassfish\modules\webservices-osgi.jar
C:\Program Files\JavaEE\glassfish\modules\webservices.security.jar
C:\Program Files\JavaEE\glassfish\modules\weld-osgi-bundle.jar
C:\Program Files\JavaEE\mq\lib\jaxm-api.jar

Glassfish4 glassfish \ domains \ domain1 \ logs \ server.log

的日志
[2015-06-30T08:48:23.755+0200] [glassfish 4.1] [INFO] [NCLS-LOGGING-00009] [javax.enterprise.logging] [tid: _ThreadID=15 _ThreadName=RunLevelControllerThread-1435646903303] [timeMillis: 1435646903755] [levelValue: 800] [[
  Running GlassFish Version: GlassFish Server Open Source Edition  4.1  (build 13)]]

[2015-06-30T08:48:23.755+0200] [glassfish 4.1] [INFO] [NCLS-LOGGING-00010] [javax.enterprise.logging] [tid: _ThreadID=15 _ThreadName=RunLevelControllerThread-1435646903303] [timeMillis: 1435646903755] [levelValue: 800] [[
  Server log file is using Formatter class: com.sun.enterprise.server.logging.ODLLogFormatter]]

[2015-06-30T08:48:24.130+0200] [glassfish 4.1] [INFO] [NCLS-SECURITY-01115] [javax.enterprise.system.core.security] [tid: _ThreadID=17 _ThreadName=RunLevelControllerThread-1435646903334] [timeMillis: 1435646904130] [levelValue: 800] [[
  Realm [admin-realm] of classtype [com.sun.enterprise.security.auth.realm.file.FileRealm] successfully created.]]

[2015-06-30T08:48:24.145+0200] [glassfish 4.1] [INFO] [NCLS-SECURITY-01115] [javax.enterprise.system.core.security] [tid: _ThreadID=17 _ThreadName=RunLevelControllerThread-1435646903334] [timeMillis: 1435646904145] [levelValue: 800] [[
  Realm [file] of classtype [com.sun.enterprise.security.auth.realm.file.FileRealm] successfully created.]]

[2015-06-30T08:48:24.161+0200] [glassfish 4.1] [INFO] [NCLS-SECURITY-01115] [javax.enterprise.system.core.security] [tid: _ThreadID=17 _ThreadName=RunLevelControllerThread-1435646903334] [timeMillis: 1435646904161] [levelValue: 800] [[
  Realm [certificate] of classtype [com.sun.enterprise.security.auth.realm.certificate.CertificateRealm] successfully created.]]

[2015-06-30T08:48:24.879+0200] [glassfish 4.1] [INFO] [] [org.glassfish.ha.store.spi.BackingStoreFactoryRegistry] [tid: _ThreadID=16 _ThreadName=RunLevelControllerThread-1435646903334] [timeMillis: 1435646904879] [levelValue: 800] [[
  Registered org.glassfish.ha.store.adapter.cache.ShoalBackingStoreProxy for persistence-type = replicated in BackingStoreFactoryRegistry]]

[2015-06-30T08:48:24.894+0200] [glassfish 4.1] [INFO] [SEC-SVCS-00100] [javax.enterprise.security.services] [tid: _ThreadID=14 _ThreadName=RunLevelControllerThread-1435646903287] [timeMillis: 1435646904894] [levelValue: 800] [[
  Authorization Service has successfully initialized.]]

[2015-06-30T08:48:25.581+0200] [glassfish 4.1] [INFO] [NCLS-CORE-00087] [javax.enterprise.system.core] [tid: _ThreadID=17 _ThreadName=RunLevelControllerThread-1435646903334] [timeMillis: 1435646905581] [levelValue: 800] [[
  Grizzly Framework 2.3.15 started in: 406ms - bound to [/0.0.0.0:8080]]]

[2015-06-30T08:48:25.643+0200] [glassfish 4.1] [INFO] [NCLS-CORE-00087] [javax.enterprise.system.core] [tid: _ThreadID=17 _ThreadName=RunLevelControllerThread-1435646903334] [timeMillis: 1435646905643] [levelValue: 800] [[
  Grizzly Framework 2.3.15 started in: 0ms - bound to [/0.0.0.0:8181]]]

[2015-06-30T08:48:25.659+0200] [glassfish 4.1] [INFO] [NCLS-CORE-00087] [javax.enterprise.system.core] [tid: _ThreadID=17 _ThreadName=RunLevelControllerThread-1435646903334] [timeMillis: 1435646905659] [levelValue: 800] [[
  Grizzly Framework 2.3.15 started in: 0ms - bound to [/0.0.0.0:4848]]]

[2015-06-30T08:48:25.752+0200] [glassfish 4.1] [INFO] [NCLS-CORE-00087] [javax.enterprise.system.core] [tid: _ThreadID=15 _ThreadName=RunLevelControllerThread-1435646903303] [timeMillis: 1435646905752] [levelValue: 800] [[
  Grizzly Framework 2.3.15 started in: 15ms - bound to [/0.0.0.0:3700]]]

[2015-06-30T08:48:25.752+0200] [glassfish 4.1] [INFO] [NCLS-CORE-00017] [javax.enterprise.system.core] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1435646905752] [levelValue: 800] [[
  GlassFish Server Open Source Edition  4.1  (13) startup time : Felix (6 366ms), startup services(2 480ms), total(8 846ms)]]

[2015-06-30T08:48:26.205+0200] [glassfish 4.1] [INFO] [NCLS-CORE-00087] [javax.enterprise.system.core] [tid: _ThreadID=16 _ThreadName=RunLevelControllerThread-1435646903334] [timeMillis: 1435646906205] [levelValue: 800] [[
  Grizzly Framework 2.3.15 started in: 0ms - bound to [/0.0.0.0:7676]]]

[2015-06-30T08:48:26.361+0200] [glassfish 4.1] [INFO] [NCLS-BOOTSTRAP-00027] [javax.enterprise.bootstrap] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1435646906361] [levelValue: 800] [[
  Registered com.sun.enterprise.glassfish.bootstrap.osgi.EmbeddedOSGiGlassFishImpl@3ed581 as OSGi service registration: org.apache.felix.framework.ServiceRegistrationImpl@1967222.]]

[2015-06-30T08:48:26.579+0200] [glassfish 4.1] [INFO] [NCLS-JMX-00005] [javax.enterprise.system.jmx] [tid: _ThreadID=54 _ThreadName=Thread-15] [timeMillis: 1435646906579] [levelValue: 800] [[
  JMXStartupService has started JMXConnector on JMXService URL service:jmx:rmi://ntbHP:8686/jndi/rmi://ntbHP:8686/jmxrmi]]

[2015-06-30T08:48:28.105+0200] [glassfish 4.1] [INFO] [] [org.hibernate.validator.internal.util.Version] [tid: _ThreadID=42 _ThreadName=admin-listener(2)] [timeMillis: 1435646908105] [levelValue: 800] [[
  HV000001: Hibernate Validator 5.0.0.Final]]

通过添加项目启动GlasFish从Elipse控制台进行日志记录:

2015-06-30T09:19:00.539+0200|Info: Running GlassFish Version: GlassFish Server Open Source Edition  4.1  (build 13)
2015-06-30T09:19:00.554+0200|Info: Server log file is using Formatter class: com.sun.enterprise.server.logging.ODLLogFormatter
2015-06-30T09:19:01.054+0200|Info: Realm [admin-realm] of classtype [com.sun.enterprise.security.auth.realm.file.FileRealm] successfully created.
2015-06-30T09:19:01.069+0200|Info: Realm [file] of classtype [com.sun.enterprise.security.auth.realm.file.FileRealm] successfully created.
2015-06-30T09:19:01.085+0200|Info: Realm [certificate] of classtype [com.sun.enterprise.security.auth.realm.certificate.CertificateRealm] successfully created.
2015-06-30T09:19:01.880+0200|Info: Authorization Service has successfully initialized.
2015-06-30T09:19:02.052+0200|Info: Registered org.glassfish.ha.store.adapter.cache.ShoalBackingStoreProxy for persistence-type = replicated in BackingStoreFactoryRegistry
2015-06-30T09:19:02.723+0200|Info: Grizzly Framework 2.3.15 started in: 421ms - bound to [/0.0.0.0:8080]
2015-06-30T09:19:02.770+0200|Info: Grizzly Framework 2.3.15 started in: 16ms - bound to [/0.0.0.0:8181]
2015-06-30T09:19:02.770+0200|Info: Grizzly Framework 2.3.15 started in: 0ms - bound to [/0.0.0.0:4848]
2015-06-30T09:19:02.848+0200|Info: Grizzly Framework 2.3.15 started in: 0ms - bound to [/0.0.0.0:3700]
2015-06-30T09:19:02.848+0200|Info: GlassFish Server Open Source Edition  4.1  (13) startup time : Felix (5�242ms), startup services(2�746ms), total(7�988ms)
2015-06-30T09:19:03.238+0200|Info: Grizzly Framework 2.3.15 started in: 0ms - bound to [/0.0.0.0:7676]
2015-06-30T09:19:03.472+0200|Info: Registered com.sun.enterprise.glassfish.bootstrap.osgi.EmbeddedOSGiGlassFishImpl@7dfd44 as OSGi service registration: org.apache.felix.framework.ServiceRegistrationImpl@2eea2a.
2015-06-30T09:19:03.768+0200|Info: JMXStartupService has started JMXConnector on JMXService URL service:jmx:rmi://ntbHP:8686/jndi/rmi://ntbHP:8686/jmxrmi
2015-06-30T09:19:05.827+0200|Info: HV000001: Hibernate Validator 5.0.0.Final

0 个答案:

没有答案