web上传图片到七牛云服务器

本文通过java web的使用,把要上传的图片通过浏览器上传到服务器上面.
` 文本仅供参考,可能出现很多不合理;


` 1 创建对应的jsp页面:
下面是jsp下面的对应的from表单,上传文件用的那么ImgFiles的属性名称,同样你可以使用其他的,或者以数组的形式,比如imgFiles[0],…

<form action="http://ipAdress:9010/api/sdingba/upload" method="post"
      enctype="multipart/form-data">
    <p>
        选择文件:<input type="file" name="imgFiles">
    <p>
        选择文件:<input type="file" name="imgFiles">
    <p>
        选择文件:<input type="file" name="imgFiles">
    <p>
        <input type="submit" value="提交">
</form>

2, 创建对应的voParam
本文主要将文件上传,不涉及其他的参数

private List<MultipartFile> imgFiles;
// set and get method ...

3,创建对应的控制器Controller
用控制器的方式,绑定参数的方式添加对应的图片文件的属性.

    @ResponseBody
    @RequestMapping("/upload")
    public BaseVO addMaterialData(AdMaterialParam param) 
    {
        List<MultipartFile> multipartFiles = adMaterialParam.getImgFiles();
        //这样就算获取到了上传的文件了,
        //如果你吧文件添加到服务器本地上面,你可以使用org.apache.commons.io下面的FileUtils对象直接吧文件复制到本地,
        //比如:FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path, filename));
    }

4,但是如果想上传到七牛云还是要做很多的处理的;如下
比如,你可能要处理文件的大小格式后在添加到云上面,等等,

本文的文件上传的格式使用的是MultipartFile属性的imgFile.getBytes()属性,
imgFile.getInputStream()可以获取流信息;
    //imageFiles是获取到的  List<MultipartFile>
    List<AdImagePath> adImagePaths = new ArrayList<>();
        AdImagePath adImagePath;
  for (int i = 0; i < imageFiles.size(); i++) {
            adImagePath = new AdImagePath();
            adImagePath.setMaterialId(adMaterialId);
            MultipartFile imgFile = imageFiles.get(i);

            try {
                boolean isImage = isImage(imgFile.getInputStream());
                if (isImage) {
                    // 处理文件 大小
                    byte[] imgBytes = setImageTypeSize(setWidth, setHeight, imgFile);

                    if (!CollectionUtils.sizeIsEmpty(imgBytes)) {
                        String resultPath = qiniuYunUtils.upload(imgBytes, imgFile.getOriginalFilename(),
                                String.valueOf(i + 1));
                        // String resultPath=qiniuYunUtils.upload(imgFile.getBytes(),imgFile.getOriginalFilename());
                        String path = JSON.parseObject(resultPath).get("key").toString();
                        adImagePath.setImageUrl(path);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
                jsonObject.put("result", "error");
            }
            adImagePaths.add(adImagePath);
        }
**以下是上面那段代码包含的函数;不足的参考上一篇博客,utils**
/**
     * 判断文件是否是图片格式
     *
     * @param imageFile
     * @return
     */
    private boolean isImage(InputStream imageFile) {
        Image img = null;
        try {
            img = ImageIO.read(imageFile);
            if (img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0) {
                return false;
            }
            return true;
        } catch (Exception e) {
            return false;
        } finally {
            img = null;
        }
    }
   /**
     * 设置图片格式的大小
     *
     * @param setWidth
     * @param setHeight
     * @param imgFile
     * @return
     * @throws IOException
     */
    private byte[] setImageTypeSize(int setWidth, int setHeight, MultipartFile imgFile) throws IOException {
        byte[] imgBytes;
        if (setWidth > 0 || setHeight > 0) {
            imgBytes = qiniuYunUtils.resizeImageFile(imgFile.getInputStream(), setWidth, setHeight);
        } else {
            imgBytes = imgFile.getBytes();
        }
        return imgBytes;
    }
 /**
     * 上传文件
     * 
     * @param file byte[]
     * @param fileName
     * @return 文件名
     */
    public static String upload(byte[] file, String fileName, String orderBy) {
        String key = orderBy + "=" + UUID.randomUUID().toString() + "_" + fileName;
        UploadManager uploadManager = new UploadManager();// 创建上传对象
        try {
            Response res = uploadManager.put(file, key, getUpToken());
            return res.bodyString();
        } catch (QiniuException e) {
            Response r = e.response;
            LOGGER.error("上传七牛云异常:", r.toString());
            try {
                LOGGER.error("上传七牛云异常:", r.bodyString());
                System.out.println();
            } catch (QiniuException e1) {
            }
        }
        return null;
    }
**下一篇博客将 七牛云的数据怎么下载.**
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值