Java NanoHTTPD:使用html表单上传文件(POST)

Java NanoHTTPD:使用html表单上传文件(POST),java,html,nanohttpd,Java,Html,Nanohttpd,我正在Java桌面环境上使用NanoHTTPD webserver 2.1.0。(没有安卓系统) 一切正常…但不是使用POST方法上传文件(表单不支持PUT) 以下是我的HTML代码: <form method="post" enctype="multipart/form-data"> choose a file<br> <input name="file" type="file" size="50" maxlength="100000"">

我正在Java桌面环境上使用NanoHTTPD webserver 2.1.0。(没有安卓系统)

一切正常…但不是使用POST方法上传文件(表单不支持PUT)

以下是我的HTML代码:

<form method="post" enctype="multipart/form-data">
    choose a file<br>
    <input name="file" type="file" size="50" maxlength="100000"">
    <button type="submit">upload</button>
</form>
如果内容是pic或二进制文件,则会出现问题

NanoHTTPD似乎没有对POST请求进行任何特殊处理。它总是一样的…将请求保存到tmp文件并提供一个页面。 因此: -如何获得正确的临时文件? -->我认为这是一个错误。我得到了一个正确的路径和名称,但是“数字”被破坏了。idk…如果发生上载,我是否应该临时更改java tmp路径,然后始终删除该文件。那么我只有一个独立于任何命名的tmp文件? -如何从文件中删除html请求头

还是我做错了什么?这是将文件上载到nanohttpd的正确方法吗


谢谢你的帮助

我在发布我的“你找到解决方案了吗?”问题后不久就想出来了,但在你今天回复之前忘了发布

<form method="post" enctype="multipart/form-data" action="http://whatever.com/path/">
    <input type="file" name="file" />
    <input type="hidden" name="extra_data" value='blah blah blah' />
    <input type="submit" value="Send" />
</form>
重要的是,您发送的数据包括文件数据、文件名和文件mime类型

在我的例子中,我使用python请求进行发布,没有向它发送足够的数据。正确的员额结构将是:

file_data = { 'file': ('test.png', open('../some_file.png', 'rb'), 'image/png') }
requests.post("http://whatever.com/path", data = { 'extra_data': "blah blah blah" }, files = file_data)
上面的代码读取自“some_file.png”,但告诉服务器它被称为“test.png”


希望有帮助

老问题!但我一直在寻找解决这个问题的办法。我在处理
IHTTPSession
输入流时遇到问题。事实证明,在读取流时,它会过早地空运行,您必须返回以获取更多。当我发送的
应用程序/octet流
数据大于500k左右时,发生了这种情况(我没有提供确切的数据)

我的解决方案是打开
IHTTPSession
输入流,反复读取输入流,直到其完全耗尽:

public Response post_upload(IHTTPSession session) {   
    System.out.println("received HTTP post with upload body...");  
    int streamLength = Integer.parseInt(session.getHeaders().get("content-length"));
    System.out.println("Content length is: " + streamLength);
    byte[] fileContent = new byte[streamLength];
    try {
        InputStream input = session.getInputStream();
        int bytesRead = 0;
        int iterations = 0;
        while (bytesRead < streamLength) {              
            int thisRead = input.read(fileContent, bytesRead, streamLength-bytesRead);                
            bytesRead += thisRead;
            iterations++;
        }
        System.out.println("Read " + bytesRead + " bytes in " + iterations + " iterations.");
    }
    catch (Exception e) {
        System.out.println("We have other problems...");
    }
    return newFixedLengthResponse(Response.Status.OK, "application/json", "[\"no prob\"]");
}
公共响应后上传(IHTTPSession会话){
System.out.println(“接收到带有上传主体的HTTP帖子…”);
int streamLength=Integer.parseInt(session.getHeaders().get(“内容长度”));
System.out.println(“内容长度为:“+streamLength”);
字节[]文件内容=新字节[streamLength];
试一试{
InputStream输入=会话。getInputStream();
int字节读取=0;
int迭代次数=0;
而(字节读取<流长度){
int thisRead=input.read(fileContent、bytesRead、streamLength bytesRead);
字节读取+=此读取;
迭代++;
}
System.out.println(“读取”+bytesRead+“+iterations+”iterations中的字节”);
}
捕获(例外e){
System.out.println(“我们还有其他问题…”);
}
返回newFixedLengthResponse(Response.Status.OK,“application/json”、“[\“no prob\”]”);
}

我希望这能有所帮助。

我知道你在7月份发布了帖子,但你有没有找到解决办法?我得到了损坏的PNG文件,因为NanoHttpd甚至对二进制文件都使用readLine(),没有sry。我没有找到解决方案:(我只是放弃了。谢谢你。你保存了我的项目。是使用了InputStream input=session.getInputStream();以错误的方式;这就是为什么出现错误响应错误400的原因。在使用你的InputStream方法后,我的问题解决了。谢谢!!!!!!!!!
if (session.getMethod() == Method.POST) {
    try { session.parseBody(files); }
    catch (IOException e1) { e1.printStackTrace(); }
    catch (ResponseException e1) { e1.printStackTrace(); }

    extraData = session.getParms().get("extra_data");
    File file = new File(files.get("file"));
}
file_data = { 'file': ('test.png', open('../some_file.png', 'rb'), 'image/png') }
requests.post("http://whatever.com/path", data = { 'extra_data': "blah blah blah" }, files = file_data)
public Response post_upload(IHTTPSession session) {   
    System.out.println("received HTTP post with upload body...");  
    int streamLength = Integer.parseInt(session.getHeaders().get("content-length"));
    System.out.println("Content length is: " + streamLength);
    byte[] fileContent = new byte[streamLength];
    try {
        InputStream input = session.getInputStream();
        int bytesRead = 0;
        int iterations = 0;
        while (bytesRead < streamLength) {              
            int thisRead = input.read(fileContent, bytesRead, streamLength-bytesRead);                
            bytesRead += thisRead;
            iterations++;
        }
        System.out.println("Read " + bytesRead + " bytes in " + iterations + " iterations.");
    }
    catch (Exception e) {
        System.out.println("We have other problems...");
    }
    return newFixedLengthResponse(Response.Status.OK, "application/json", "[\"no prob\"]");
}