开发者社区> 问答> 正文

Java Struts2 接口如何获取POST过来的文件?

POSTMAN中的form-data参数
screenshot
试过request.getReader(),但是执行这句的时候就会报错
请问各位有什么方法才能接收到POST过来的图片,并且保存在某个路径下

展开
收起
蛮大人123 2016-03-10 17:56:29 2994 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪
    private File[] upload;
    private String[] uploadFileName;
    if (upload != null) {
        String fileNames = "";
                String fileAdds = "";
                try {
                    for (int i = 0; i < upload.length; i++) {
                        InputStream is = new FileInputStream(upload[i]);
                        String epName = uploadFileName[i].substring(uploadFileName[i].lastIndexOf("."));
                        String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()).toString() + i + epName;
                        String path = this.getServletRequest().getServletContext().getRealPath("/uploadfile/committee")+ "/" + fileName;
                        OutputStream os = new FileOutputStream(path);
                        byte buffer[] = new byte[(int) upload[i].length()];
                        int count = 0;
                        while ((count = is.read(buffer)) > 0) {
                            os.write(buffer, 0, count);
                        }
                        os.close();
                        is.close();
                        fileNames += uploadFileName[i] + "|";
                        fileAdds += "uploadfile/committee/" + fileName + "|";
                    }
                } catch (Exception e) {
                }
                advice.setFileNames(fileNames);
                advice.setFileAdds(fileAdds);
            }
    2019-07-17 18:58:04
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载