前台使用easyui框架、控制层使用springmvc文件上传的demo

废话不多说,上代码:

jsp页面:

<form action="fileUpload1.do" enctype="multipart/form-data"
        method="post">
        <div style="margin-bottom: 20px">
            <div>File1:</div>
            <input class="easyui-filebox" name="sourceFile"
                data-options="prompt:'Choose a file...'" style="width: 20%">
        </div>
        <input type="submit" value="上传">
    </form>

控制层springmvc接收并处理:

public String fileUpload(FileUpload file) {
        MultipartFile multipartFiles = file.getSourceFile();
        if (multipartFiles != null) {
            try {
                byte[] by = multipartFiles.getBytes();
                WriteToDiskImpl.writeToDiskBytes(by, "e://abc.txt");
            } catch (IOException e) {
                try {
                    throw new CustomException("读取文件失败");

                } catch (CustomException e1) {
                    e1.printStackTrace();
                }
            }
        }
        System.out.println("111");
        return "sucess";

 

FileUpload 的pojo

public class FileUpload implements Serializable {
    private Integer id;
      private String name;
      private String description;
      private String url;
      private String icon;
      private MultipartFile sourceFile;

setget().....方法

具体操作写的方法
    @Override
    public void writeToDiskBytes(byte[] bytes, String path) throws IOException {

        FileOutputStream fo = new FileOutputStream(path);
        fo.write(bytes);

        fo.close();

    }

 

需要注意几点:

1.必须以Post方式提交

2.文件类型为: multipart/form-data

3.jsp页面上传文件的input 的name与pojo的属性保持一致

 

 

 

 

 

 

 

 

转载于:https://my.oschina.net/begin01running/blog/740613

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值