使用httpclient4.3.1 上传文件的同时添加普通参数,服务端取不到参数
澳门新葡京官网
澳门新葡京官网
当前位置 : 澳门新葡京官网 > 新葡京娱乐场

使用httpclient4.3.1 上传文件的同时添加普通参数,服务端取不到参数

<dependency>

<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>

</dependency>
<dependency>

<groupId>org.apache.httpcomponents</groupId>  
<artifactId>httpclient</artifactId>  
<version>4.3.1</version>  

</dependency>

<dependency>

<groupId>org.apache.httpcomponents</groupId>  
<artifactId>httpcore</artifactId>  
<version>4.3.1</version>  

</dependency>
<dependency>

<groupId>org.apache.httpcomponents</groupId>  
<artifactId>httpmime</artifactId>  
<version>4.3.1</version>  

</dependency>

<dependency>

<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>

</dependency>

客户端

    // 创建默认的httpClient实例.
    HttpClient httpclient = HttpClients.createDefault;
    // 创建httppost
    HttpPost httppost = new HttpPostUploadService;
    // 创建参数队列
    FileBody bin = new FileBodynew File"D:\\download\\1.jpg";
    // StringBody pathBody = new
    // StringBody"/20170317/",ContentType.DEFAULT_TEXT;

    HttpEntity reqEntity = MultipartEntityBuilder.create
            .setModeHttpMultipartMode.BROWSER_COMPATIBLE
            .addBinaryBody"uploadFile", new File"D:\\download\\1.jpg"
            .addTextBody"path", "/20170317/".build;
    try {
        httppost.setEntityreqEntity;
        System.out.println"executing request " + httppost.getURI;
        HttpResponse response = httpclient.executehttppost;
        try {
            HttpEntity entity = response.getEntity;
            if entity != null {
                System.out
                        .println"--------------------------------------";
                System.out.println"Response content: "
                        + EntityUtils.toStringentity, "UTF-8";
                System.out
                        .println"--------------------------------------";
            }

        } catch ClientProtocolException e {
            e.printStackTrace;
        }
    } catch UnsupportedEncodingException e1 {
        e1.printStackTrace;
    } catch IOException e {
        e.printStackTrace;
    }

服务端

    try {  
        System.out.println"IP:" + request.getRemoteAddr;  
        String path= request.getParameter"path";
        Part part1=  request.getPart"path";
        Iterator<Part>iterator=request.getParts.iterator;  
        
        while iterator.hasNext {//遍历客户端上传的所有文件                
            Part part = Part iterator.next;   
            System.out.printlnpart.getContentType;
        }  
        String username =request.getParameter"username";
        System.out.println"username:" + username;
        // 1、创建工厂类:DiskFileItemFactory  
        DiskFileItemFactory facotry = new DiskFileItemFactory;  
        String tempDir = getServletContext.getRealPath"/WEB-INF/upload";  
        facotry.setRepositorynew FiletempDir;//设置临时文件存放目录  
        // 2、创建核心解析类:ServletFileUpload  
        ServletFileUpload upload = new ServletFileUploadfacotry;  
        upload.setHeaderEncoding"UTF-8";// 解决上传的文件名乱码  
        upload.setFileSizeMax1024 * 1024 * 1024;// 单个文件上传最大值是1M  
        upload.setSizeMax2048 * 1024 * 1024;//文件上传的总大小限制  

        // 3、判断用户的表单提交方式是不是multipart/form-data  
        boolean bb = upload.isMultipartContentrequest;  
        if !bb {  
            response.setCharacterEncoding"utf-8";
            response.getWriter.print"不是multipart/form-data";
            return;  
        }  
        // 4、是:解析request对象的正文内容List<FileItem>  
        List<FileItem> items = upload.parseRequestrequest;  

      String dirString =path.substring0,path.lastIndexOf"/";
       String filenameString=path.substringpath.lastIndexOf"/"+1;
        String storePath = getServletContext.getRealPathdirString;// 上传的文件的存放目录  
        for FileItem item : items {  
            if item.isFormField {  
                // 5、判断是否是普通表单:打印看看  
                String fieldName = item.getFieldName;// 请求参数名  
                String fieldValue = item.getString"UTF-8";// 请求参数值  
                System.out.printlnfieldName + "=" + fieldValue;  
            } else {  
                // 6、上传表单:得到输入流,处理上传:保存到服务器的某个目录中,保存时的文件名是啥?  
                String fileName = item.getName;// 得到上传文件的名称 C:\Documents  
                                                    // and  
                                                    // Settings\shc\桌面\a.txt  
                                                    // a.txt  
                //解决用户没有选择文件上传的情况  
                iffileName==null||fileName.trim.equals""{  
                    continue;  
                }  
                fileName = fileName  
                        .substringfileName.lastIndexOf"\\" + 1;  
                String newFileName = fileName;  
                System.out.println"上传的文件名是:" + filenameString;  
                InputStream in = item.getInputStream;  
                String savePath = makeDirstorePath, filenameString;  
                OutputStream out = new FileOutputStreamsavePath;  
                byte b[] = new byte[1024];  
                int len = 0;  
                while len = in.readb != -1 {  
                    out.writeb, 0, len;  
                }  
                in.close;  
                out.close;  
                item.delete;//删除临时文件  
                
                response.getWriter.printfileName;
            }  
        }  
    }catchFileUploadBase.FileSizeLimitExceededException e{  
        response.getWriter.print"单个文件大小不能超出5M";
        request.setAttribute"message", "单个文件大小不能超出5M";  
        request.getRequestDispatcher"/message.jsp".forwardrequest,  
                response;  
    }catchFileUploadBase.SizeLimitExceededException e{  
        response.getWriter.print"总文件大小不能超出7M";
}catch Exception e {  
        e.printStackTrace;  
        response.getWriter.print"上传失败";
    }  

服务端 String path= request.getParameter"path"; 为null。
Part part1= request.getPart"path"; 这个是有的 但是变成流了

栏目列表

广告位

澳门新葡京官网