Illegal error when compiling an http server based on sun.com.net.http in java

0

the issue is as follows. When compiling a built-in server that I have under the sun.com.net library. * It gives me a very strange error, it always went well and I have not changed the code of which is the following:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Map.Entry;

import org.apache.commons.fileupload.FileItem;

import org.apache.commons.fileupload.RequestContext;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class HttpServerTest {

    public static void main(String[] args) throws Exception {
        HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
        server.createContext("/fileupload", new MyHandler());
        server.setExecutor(null); // creates a default executor
        server.start();
    }

    static class MyHandler implements HttpHandler {
        @Override
        public void handle(final HttpExchange t) throws IOException {
            for(Entry<String, List<String>> header : t.getRequestHeaders().entrySet()) {
                System.out.println(header.getKey() + ": " + header.getValue().get(0));
            }
            DiskFileItemFactory d = new DiskFileItemFactory();      

            try {
                ServletFileUpload up = new ServletFileUpload(d);
                List<FileItem> result = up.parseRequest(new RequestContext() {

                    @Override
                    public String getCharacterEncoding() {
                        return "UTF-8";
                    }

                    @Override
                    public int getContentLength() {
                        return 0; //tested to work with 0 as return
                    }

                    @Override
                    public String getContentType() {
                        return t.getRequestHeaders().getFirst("Content-type");
                    }

                    @Override
                    public InputStream getInputStream() throws IOException {
                        return t.getRequestBody();
                    }

                });
                t.getResponseHeaders().add("Content-type", "text/plain");
                t.sendResponseHeaders(200, 0);
                OutputStream os = t.getResponseBody();               
                for(FileItem fi : result) {
                    os.write(fi.getName().getBytes());
                    os.write("\r\n".getBytes());
                    System.out.println("File-Item: " + fi.getFieldName() + " = " + fi.getName());
                }
                os.close();

            } catch (Exception e) {
                e.printStackTrace();
            }            
        }
    }
}

The error that the compiler throws me is the following:

COMPILATION ERROR : 
-------------------------------------------------------------
bfhsoftware/sonidoambiental/servidorhttp.java:[313,43] cannot access javax.servlet.http.HttpServletRequest
  class file for javax.servlet.http.HttpServletRequest not found
1 error

You can see that it asks for the library javax.servlet.http.HttpServletRequest which I do not need it because I use as I have already named the library server com.sun.net.httpserver.HttpHandler

The line that gives an error is the following:

        List<FileItem> result = up.parseRequest(new RequestContext() {

It does not make sense, if I add the library that asks me if it compiles but it does not work. For that reason I say that it is an illogical error. Someone who has more knowledge than I know what happens? PD: I had previously tried to add a server of this nature, the library that asks me, but delete the dependencies and everything, and I keep asking, I have tried two different IDE, netbeans and intellij idea comunity version, both tell me the same mistake, it will be that the library is present somewhere and I do not know? I hope you can help me thanks

    
asked by Bernardo Harreguy 15.02.2018 в 01:43
source

1 answer

1

You are using the apache commons fileupload library. If you look at the dependencies of that library in the following URL

Requests:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.4</version>
    <scope>provided</scope>
</dependency>

This dependency is usually provided by the application server, if you are not using one then you need the jar of javaEE-api.

    
answered by 15.02.2018 / 02:40
source