关于android app上传文件服务端的搭建

app端上传文件的例子很多,retrofit2.0貌似很火,也可以实现文件上传,之前参考网上的资料,app端明明都没错了,但就是上传不了文件。无奈之下请教媳妇,她说java的servlet要在web.Xml设置上传的路径。。。。。。

下面就分享我设置的过程:

由于我了解servlet,网上很多也是用java实现,所以就安装了myeclipse,准备在本机实现服务端来接收app上传的文件。

1、创建一个web project,包里新建一个java类继承httpservlet,并添加2个jar包到webroot-WEB-INF-lib目录下:



2、java类处理文件上传,代码参考网上:

public classUploadServlet extends HttpServlet{

    private static final long serialVersionUID = 1L;

    private String path;

扫描二维码关注公众号,回复: 940753 查看本文章

    public UploadServlet() {

        super();

    }

    public voiddoGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {

        System.out.println("xxxx");

        this.doPost(request,response);

    }

    protected voiddoPost(HttpServletRequest request, HttpServletResponse response)

            throwsServletException, IOException {

        response.setContentType("text/html;charset=utf-8");

        request.setCharacterEncoding("utf-8");

        response.setCharacterEncoding("utf-8");

        PrintWriter out = response.getWriter();

       

        // 创建文件项目工厂对象

        DiskFileItemFactory factory = newDiskFileItemFactory();

        // 设置文件上传路径

        String upload = this.getServletContext().getRealPath("/");

       

        // 获取系统默认的临时文件保存路径,该路径为Tomcat根目录下的temp文件夹

        String temp = System.getProperty("java.io.tmpdir");

        // 设置缓冲区大小为 5M

        factory.setSizeThreshold(1024 * 1024 *5);

        // 设置临时文件夹为temp

        factory.setRepository(newFile(temp));

        // 用工厂实例化上传组件,ServletFileUpload 用来解析文件上传请求

        ServletFileUpload servletFileUpload = newServletFileUpload(factory);

        // 解析结果放在List

        try {

            List<FileItem> list =servletFileUpload.parseRequest(request);

            for (FileItemitem : list) {

                String name =item.getFieldName();

                InputStream is =item.getInputStream();

                if(name.contains("content")) {

                   System.out.println(inputStream2String(is));

                } else if(name.contains("img")) {

                   try {

                       path = upload+"\\"+item.getName();

                       inputStream2File(is, path);

                       break;

                   } catch(Exception e) {

                       e.printStackTrace();

                   }

                }

            }

            out.write(path);  //这里我把服务端成功后,返回给客户端的是上传成功后路径

        } catch(FileUploadException e) {

            e.printStackTrace();

            System.out.println("failure");

            out.write("failure");

        }

        out.flush();

        out.close();

    }

    // 流转化成字符串

    public static StringinputStream2String(InputStream is) throws IOException {

        ByteArrayOutputStream baos = newByteArrayOutputStream();

        int i = -1;

        while ((i =is.read()) != -1) {

            baos.write(i);

        }

        returnbaos.toString();//

    }

    // 流转化成文件

    public static void inputStream2File(InputStreamis, String savePath) throws Exception {

        System.out.println("文件保存路径为:" + savePath);

        File file = newFile(savePath);

        InputStream inputSteam = is;

        BufferedInputStream fis = newBufferedInputStream(inputSteam);

        FileOutputStream fos = newFileOutputStream(file);

        int f;

        while ((f =fis.read()) != -1) {

            fos.write(f);

        }

        fos.flush();

        fos.close();

        fis.close();

        inputSteam.close();

    }

}


3、配置webroot-WEB-INF-lib下的web.xml,这个很重要,app上传的路径就在这里。

*添加servlet节点,并添加servlet-name和servlet-class,servlet-name就是继承httpservlet的类名,servlet-class是包含包名。

*添加servlet-mapping节点, url-pattern的值就是提供给app上传的路径。



4、这样就有了app上传的url:url = "http://192.168.1.100:8080/TomcatTest/fileupload"

192.168.1.100:8080是我本机的ip和端口,TomcatTest是工程名,fileupload就是web.xml设置的url-pattern。


5、另外,说一下配置工程到tomcat:







如果要进入debug:



这样应该就可以了,最后就是运行服务。

因为对服务端端不熟,哪里有不对或其它问题请指正。

猜你喜欢

转载自blog.csdn.net/cwwei20122012/article/details/78649847