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