Httpclient上传文件流 接收的问题

发布于 2022-09-06 02:27:30 字数 669 浏览 18 评论 0

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

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蘸点软妹酱 2022-09-13 02:27:30

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

安静被遗忘 2022-09-13 02:27:30

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

廻憶裏菂餘溫 2022-09-13 02:27:30
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;
        }
    }

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

浅紫色的梦幻 2022-09-13 02:27:30
//========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);
〃温暖了心ぐ 2022-09-13 02:27:30

@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";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文