javaweb File Transfer examples

The first requires two packages, there are links to two packages https://download.csdn.net/download/qq_35986709/10893851
file upload requirements page

  1. Forms must be used, not a hyperlink;
  2. Form method must be POST, but can not be GET;
  3. The form must be enctype multipart / form-data;
  4. Added in the form file form field, i.e.
    Servlet requirements:
    l can no longer use request.getParameter () to get the form data;
    l may be used request.getInputStream () to get all of the form data, rather than a single table of data;
    l this means do not use fileupload, we need to own content request.getInputStream () to parse! ! !
 <form action="${pageContext.request.contextPath }/FileUploadServlet" method="post" enctype="multipart/form-data">
        	用户名:<input type="text" name="username"/><br/>
        	文件1:<input type="file" name="file1"/><br/>
        	文件2:<input type="file" name="file2"/><br/>
        	<input type="submit" value="提交"/>    
        	</form>
 DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
            //解析工厂类
            ServletFileUpload servletFileUpload=new ServletFileUpload(diskFileItemFactory);
            //设置编码为utf-8,以防乱码
            servletFileUpload.setHeaderEncoding("utf-8");
            try {
                //解析request请求
                List<FileItem> fileItems = servletFileUpload.parseRequest(request);
                //定义map用来接收表单数据
                LinkedHashMap<String,String> formMap=new LinkedHashMap<String,String>();
                //遍历接收到的数据
                for (FileItem fileItem : fileItems) {
                    //判断是是表单文件,如果不是表单文件,则创建文件写入文件数据
                    if (!fileItem.isFormField()){
                        if (fileItem.get().length>0&&fileItem.getFieldName()!=null) {
                            String realpath = request.getServletContext().getRealPath("/static");
                            File file = new File(realpath + "/wapian/images/logo.png");
                            if (!file.exists())
                            file.createNewFile();
                            fileItem.write(file);
                        }//否则就是表单文件
                    }else {
                        formMap.put(fileItem.getFieldName(),new String(fileItem.get(),"UTF-8"));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

There is also relates to a method of FileItem
 String getName (): Get File file name field;
l String getString (): Gets contents of the field, if the file field, then get the file content, of course, must be a text file uploaded file;
l String getFieldName (): Gets the field name, for example:, returns the username;
l String getContentType (): Gets the uploaded file type, for example: text / plain.
 Long getSize (): Get upload file size;
l Boolean isFormField (): determining whether the current form field is a normal text field, if it returns false, description file field;
l the InputStream the getInputStream (): Get the uploaded file corresponding to the input stream ;
l void the Write (file): to save the uploaded file to the specified file.
byte [] get (): array of bytes of data obtained

Published 34 original articles · won praise 6 · views 3668

Guess you like

Origin blog.csdn.net/qq_35986709/article/details/85873817