首页 > 代码库 > 文件流数组上传到七牛云

文件流数组上传到七牛云

首先要注册七牛云,申请个空间,获得密钥、等东西,引入官方sdk包,然后再开发文档中找到代码copy进去就可以了。

因为非常简单,我就直接弄上代码了。

  1 package com.qianmo.foru.bean.request;
  2 
  3 import com.qianmo.core.common.CommonRequest;
  4 import org.hibernate.validator.constraints.NotBlank;
  5 import org.springframework.web.bind.annotation.RequestParam;
  6 import org.springframework.web.multipart.MultipartFile;
  7 
  8 /**
  9  * Created Author: anpei
 10  * Created Time 2017/5/31 0031.
 11  * 用户下单请求
 12  */
 13 public class InsertOrdersRequest extends CommonRequest {
 14     @NotBlank(message = "订单类型不能为空")
 15     private String orderType;//00:跑腿 01:拼车 02:技术 03:其他  04:提现
 16     @NotBlank(message = "订单标题不能为空")
 17     private String demandTitle;//需求标题
 18     @NotBlank(message = "订单内容不能为空")
 19     private String demandContent;//需求内容
 20     @NotBlank(message = "订单地址不能为空")
 21     private String orderAddress;//我的地址
 22     @NotBlank(message = "交易金额不能为空")
 23     private String orderTxnAmt;//交易金额
 24     @NotBlank(message = "手机号不能为空")
 25     private String moblNo;//手机号
 26     @NotBlank(message = "下单人编号为空")
 27     private String userId;//下单人编号
 28 
 29     private String goodsBudget;//商品预算
 30 
 31     private  MultipartFile[] multipartFiles;//多个文件流
 32 
 33     private String favorPrice;//订单交易金额
 34     private String demandPic;//上传图片
 35     private String remark;//是否加急推送 00:加急推送 01:非加急推送
 36     private String testPurpose;//测试环境 no:测试环境 yes:真实环境
 37 
 38     public String getTestPurpose() {
 39         return testPurpose;
 40     }
 41 
 42     public void setTestPurpose(String testPurpose) {
 43         this.testPurpose = testPurpose.trim();
 44     }
 45     //order_stat 订单状态 00:初始化订单状态01:失败订单02:成功订单03:撤销订单04:订单处理中
 46 //txn_type 交易类型 支付:00 提现:01 退款:02 用户余额支付类型:88
 47 
 48     //   需求类型、需求标题、需求内容、上传图片、我的地址、跑腿费用、商品预算、手机号、是否加急推送、
 49     //   需求类型、需求标题、需求内容、上传图片、我的地址、价格、手机号
 50 
 51 
 52     public String getOrderType() {
 53         return orderType;
 54     }
 55 
 56     public void setOrderType(String orderType) {
 57         this.orderType = orderType.trim();
 58     }
 59 
 60     public String getDemandTitle() {
 61         return demandTitle;
 62     }
 63 
 64     public void setDemandTitle(String demandTitle) {
 65         this.demandTitle = demandTitle.trim();
 66     }
 67 
 68     public String getDemandContent() {
 69         return demandContent;
 70     }
 71 
 72     public void setDemandContent(String demandContent) {
 73         this.demandContent = demandContent.trim();
 74     }
 75 
 76     public String getOrderAddress() {
 77         return orderAddress;
 78     }
 79 
 80     public void setOrderAddress(String orderAddress) {
 81         this.orderAddress = orderAddress.trim();
 82     }
 83 
 84     public String getFavorPrice() {
 85         return favorPrice;
 86     }
 87 
 88     public void setFavorPrice(String favorPrice) {
 89         this.favorPrice = favorPrice.trim();
 90     }
 91 
 92     public String getMoblNo() {
 93         return moblNo;
 94     }
 95 
 96     public void setMoblNo(String moblNo) {
 97         this.moblNo = moblNo.trim();
 98     }
 99 
100     public String getGoodsBudget() {
101         return goodsBudget;
102     }
103 
104     public void setGoodsBudget(String goodsBudget) {
105         this.goodsBudget = goodsBudget.trim();
106     }
107 
108     public String getDemandPic() {
109         return demandPic;
110     }
111 
112     public void setDemandPic(String demandPic) {
113         this.demandPic = demandPic.trim();
114     }
115 
116     public String getRemark() {
117         return remark;
118     }
119 
120     public void setRemark(String remark) {
121         this.remark = remark.trim();
122     }
123 
124     public String getUserId() {
125         return userId;
126     }
127 
128     public void setUserId(String userId) {
129         this.userId = userId.trim();
130     }
131 
132     public String getOrderTxnAmt() {
133         return orderTxnAmt;
134     }
135 
136     public void setOrderTxnAmt(String orderTxnAmt) {
137         this.orderTxnAmt = orderTxnAmt.trim();
138     }
139 
140     public MultipartFile[] getMultipartFiles() {
141         return multipartFiles;
142     }
143 
144     public void setMultipartFiles(MultipartFile[] multipartFiles) {
145         this.multipartFiles = multipartFiles;
146     }
147 }

 

这是我写的一个接收ios或者android端传过来的值,文件流数组与其他参数在一起接收,下面是循环数组,实现多张图片上传。

 1  logger.info("进行图片上传");
 2         StringBuffer imgPath = new StringBuffer();
 3         if (null != request.getMultipartFiles()) {
 4             logger.info("——————有图片");
 5             for (MultipartFile file : request.getMultipartFiles()) {
 6                 imgPath.append(ImageUploadUtil.upload(file));
 7                 imgPath.append(",");
 8             }
 9         }else {
10             logger.info("——————没有图片");
11             imgPath.append(Constants.QiniuyunPictures.imgPaths+"aaa.png");
12         }
13         logger.info("——————图片上传成功");
ImageUploadUtil.upload(file); 这句代码是我把上传图片写成了一个工具类,下面是工具类的代码。
 1 package com.qianmo.foru.common;
 2 
 3 import com.google.gson.Gson;
 4 import com.qianmo.core.util.QMDateUtil;
 5 import com.qianmo.core.util.QMuuId;
 6 import com.qiniu.common.QiniuException;
 7 import com.qiniu.common.Zone;
 8 import com.qiniu.http.Response;
 9 import com.qiniu.storage.Configuration;
10 import com.qiniu.storage.UploadManager;
11 import com.qiniu.storage.model.DefaultPutRet;
12 import com.qiniu.util.Auth;
13 import org.springframework.web.multipart.MultipartFile;
14 
15 import java.io.IOException;
16 import java.io.InputStream;
17 
18 /**
19  * Created Author: anpei
20  * Created Time 2017/5/31 0031.
21  * 图片上传 oqt9s3huf.bkt.clouddn.com
22  */
23 public class ImageUploadUtil {
24     public static String upload(MultipartFile multipartFile){
25         String imgPath = "";
26 
27         //构造一个带指定Zone对象的配置类
28         Configuration cfg = new Configuration(Zone.zone0());
29 //...其他参数参考类注释
30         UploadManager uploadManager = new UploadManager(cfg);
31 //...生成上传凭证,然后准备上传
32         String accessKey = Constants.QiniuyunPictures.accessKey;//A key
33         String secretKey = Constants.QiniuyunPictures.secretKey;//S key
34         String bucket = Constants.QiniuyunPictures.bucketName;//foru 七牛云空间名
35 //默认不指定key的情况下,以文件内容的hash值作为文件名
36         String key = String.valueOf(QMDateUtil.getYYYYMMDDHHMMSS())+ QMuuId.getFixLenthString(5)+"foruApp.jpg";
37         try {
38             //   byte[] uploadBytes = "hello qiniu cloud".getBytes("utf-8");
39             //   ByteArrayInputStream byteInputStream=new ByteArrayInputStream(uploadBytes);
40             Auth auth = Auth.create(accessKey, secretKey);
41             String upToken = auth.uploadToken(bucket);
42             try {
43                 InputStream inputStream = multipartFile.getInputStream();
44                 Response response = uploadManager.put(inputStream,key,upToken,null, null);
45                 //解析上传成功的结果
46                 DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
47                 System.out.println(putRet.key);
48                 System.out.println(putRet.hash);
49             } catch (QiniuException ex) {
50                 Response r = ex.response;
51                 System.err.println(r.toString());
52                 try {
53                     System.err.println(r.bodyString());
54                 } catch (QiniuException ex2) {
55                     //ignore
56                 }
57             } catch (IOException e) {
58                 e.printStackTrace();
59             }
60         } catch (Exception ex) {
61             //ignore
62             ex.printStackTrace();
63         }
64         key = Constants.QiniuyunPictures.imgPaths+key;
65         return key;
66     }
67 }

 

 

 

文件流数组上传到七牛云