Httpclient上传文件流 接收的问题

1,做了一个上传文件的工具类 使用的是 Apache的 httpclient,传文件服务端已经成功了,但是传一个文件的输入流给服务器,服务器 使用 @RequestParam("uploadFile") MultipartFile file作为方法参数 接收不到,请问我该怎么接收传过来的输入流呢?

clipboard.png
上图是传递File 对象的客户端

clipboard.png
上图是服务端接收文件

clipboard.png
这个是传递文件输入流的客户端

但是我不知道服务端怎么接收流

阅读 13.5k
5 个回答

clipboard.png
感谢各位的回答,只需要在客户端加上相应的参数就行了

新手上路,请多包涵

是否报错呢,服务器端断点调试一下,看一下方法有没有进去。

新手上路,请多包涵
public static String FilesUpload_transferTo_spring(MultipartFile multipartFile, String filePath) {
        // 获取文件后缀
        String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
        //filePath+fileName 复合文件名
        String absolutePath = getAndSetAbsolutePath(filePath, suffix);
//            // 获取相对路径
//            String relativePath = getRelativePath(filePath, suffix);
        try {
            //save file
            multipartFile.transferTo(new File(absolutePath));
            //return relative Path
            return absolutePath;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

我之前用到过的一个接收方式,不知道适不适合你的情况.

//========MultipartEntity    
HttpPost post3 = new HttpPost("http://remotehost/post.do");    
File upfile = new File("C:\\test.jpg");    
MultipartEntity entity3 = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName("UTF-8"));    
entity3.addPart("file_name",     new StringBody(upfile.getName()));    
entity3.addPart("file_contents", new FileBody(upfile));    
post3.setEntity(entity3);    
    
new DefaultHttpClient().execute(post3);

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)

public String uploadFile(HttpServletRequest request) throws InterruptedException {
    String dept=request.getParameter("department");//获取所属部门,将文件存放到所对应的路径
   **MultipartFile file = ((MultipartHttpServletRequest) request).getFile("file");**
        String fileName=file.getOriginalFilename().substring(0,file.getOriginalFilename().indexOf("."));
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
        if(!"cpt".equals(suffix)){
            return "'"+fileName+"."+suffix+"'"   +"的文件类型是不被支持的";
        }
        File filePath=new File("d:\\template\\"+dept);
        if(!filePath.exists()){
            filePath.mkdir();
        }
        File file1 =new File(filePath,fileName+".cpt");
        try {
            file.transferTo(file1);
        } catch (IOException e) {
            log.error("文件上传失败>>>>>"+fileName+file.getOriginalFilename());
            e.printStackTrace();
            return "error";
        }
    return "success";
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题