NanoHttpd保存上传的文件 [英] NanoHttpd save uploaded files

查看:213
本文介绍了NanoHttpd保存上传的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了很多线程,但是找不到我的问题的答案...
因此,我可以在设备上启动网络服务器,并且当我尝试上传文件时,浏览器会显示成功上传",但是我无法在设备上找到该文件,也不知道是否将其上传到设备上. 我已经设置了所有权限,并在serve方法中区分了postget.

I have looked at many threads, but I can't find an answer to my question...
So I could start the webserver on my device, and when I try to upload a file the browser says "upload successfully", but I couldn't find the file on my device and I don't know whether it is uploaded to the device. I have set all permissions and distinguish between post and get in my serve method.

我认为我必须保存参数Map<String, String>文件中的上传文件.
我该怎么办?这是正确的方法吗?

I think I have to save the uploaded files from the parameter Map<String, String> files.
How could I do that? Is it the right way?

这是我的代码段:

private class MyHTTPD extends NanoHTTPD {

    public MyHTTPD() throws IOException {
        super(PORT);
    }
    public Response serve(String uri, Method method, Map<String, String> headers, Map<String, String> parms, Map<String, String> files) {           
        if (method.equals(Method.GET)) {
            return get(uri, method, headers, parms, files);
        }
        return post(uri, method, headers, parms, files);
    }

    public Response get(String uri, Method method, Map<String, String> headers, Map<String, String> parms, Map<String, String> files) {
        String get = "<html><body><form name='up' method='post' enctype='multipart/form-data'>"
                + "<input type='file' name='file' /><br /><input type='submit'name='submit' "
                + "value='Upload'/></form></body></html>";
        return new Response(get);
    }

    public Response post(String uri, Method method, Map<String, String> headers, Map<String, String> parms, Map<String, String> files) {
        String post = "<html><body>Upload successfull</body></html>";
        return new Response(post);

    }
}

推荐答案

我知道,这是一个很晚的答复,但我将答案发布以供将来参考.

I know, this is a very late response but I am posting the answer for future reference.

NanoHttpd自动上传文件并保存在缓存目录中,并在文件和参数映射中返回信息(名称,路径等).在serve方法中编写以下代码.

NanoHttpd automatically upload the file and saved in cache directory and return information (name,path etc) in files and params map. Write the following code in serve method.

File dst = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() +"/"+ parameters.get("myfile"));
File src = new File(files.get("myfile"));
try {
      Utils.copy(src, dst);
}catch (Exception e){ e.printStackTrace();}

实用程序副本

public static void copy(File src, File dst) throws IOException {
    InputStream in = new FileInputStream(src);
    OutputStream out = new FileOutputStream(dst);

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

这篇关于NanoHttpd保存上传的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆