(畢設)SSM+redis+shiro_(五)

(畢設)SSM+redis+shiro_(五)

來自專欄 java後台的學習分享

完成了多表聯查和數據插入(後面準備 多表聯合的更新)

今天準備完成集成文件上傳模塊

主要是實現圖片上傳(用戶頭像的上傳)

開始整合流程

添加依賴(阿里的和七牛的一起添加了)

<!--阿里雲OSS管理依賴-->

<dependency>

<groupId>com.aliyun.oss</groupId>

<artifactId>aliyun-sdk-oss</artifactId>

<version>2.8.3</version>

</dependency>

<!--七牛雲文件上傳依賴-->

<dependency>

<groupId>com.qiniu</groupId>

<artifactId>qiniu-java-sdk</artifactId>

<version>7.2.11</version>

<scope>compile</scope>

</dependency>

<dependency>

<groupId>com.squareup.okhttp3</groupId>

<artifactId>okhttp</artifactId>

<version>3.3.1</version>

<scope>compile</scope>

</dependency>

<dependency>

<groupId>com.google.code.gson</groupId>

<artifactId>gson</artifactId>

<version>2.6.2</version>

<scope>compile</scope>

</dependency>

<dependency>

<groupId>com.qiniu</groupId>

<artifactId>happy-dns-java</artifactId>

<version>0.1.4</version>

<scope>compile</scope>

</dependency>

(編寫SQL語句)

<!--文件上傳-->

<update id="upload" parameterType="com.sojson.common.model.UUser" >

update u_user

set

head_picture = #{head_picture,jdbcType=VARCHAR}

where id = #{id,jdbcType=BIGINT}

</update>

本次使用七牛雲圖片存儲(後面再加一個阿里雲的OSS)

接下來就是核心API介面

public class QIniuUpload {

//設置好賬號的ACCESS_KEY和SECRET_KEY

String ACCESS_KEY = "XXXXXX";

String SECRET_KEY = " XXXXXX ";

//要上傳的空間

String bucketname = " XXXXXX ";

//上傳到七牛後保存的文件名

//String key = "head_fv_.png";

//上傳文件的路徑

// String FilePath = "D:\Code_Audit_360\Windows壁紙\星空.jpg";

private String CDN_DOMAIN_NAME =" XXXXXX ";

//上傳根目錄

private String IMG_UPLOAD_PATH = "";

//第二種方式: 自動識別要上傳的空間(bucket)的存儲區域是華東、華北、華南。

private Zone z = Zone.autoZone();

private Configuration c = new Configuration(z);

//創建上傳對象

private UploadManager uploadManager = new UploadManager(c);

public String uploadImg2QiNiu(MultipartFile file) throws IOException {

//SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH:mm:ss.SSS");

String name_file = CharacterUtils.getRandomString2(8);//隨機數

String key = IMG_UPLOAD_PATH +name_file+"fv_.png";

try {

Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);

String upToken = auth.uploadToken(bucketname);

Response response = uploadManager.put(file.getInputStream(), key, upToken,null, null);

//解析上傳成功的結果

DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);

return CDN_DOMAIN_NAME + putRet.key;

} catch (QiniuException e) {

Response r = e.response;

// 請求失敗時列印的異常的信息

//System.out.println(r.toString());

try {

// 響應的文本信息

System.out.println(r.bodyString());

} catch (QiniuException qe) {

e.printStackTrace();

}

return "上傳圖片異常!";

}

}

}

control層

//文件上傳

@RequestMapping(value = "upload",method=RequestMethod.POST)

@ResponseBody

public Map<String,Object> upload(HttpServletRequest request, MultipartFile file)throws Exception {

resultMap.put("status", 400);

if (checkSuffix(file.getOriginalFilename())) {

QIniuUpload qIniuUpload = new QIniuUpload();

String head = qIniuUpload.uploadImg2QiNiu(file);

System.out.println(head);

UUser user = new UUser(head,TokenManager.getToken().getId());

userService.upload(user);

System.out.println("gooood");

resultMap.put("message", "頭像上傳成功!");

resultMap.put("status", 200);

return resultMap;

}else{

resultMap.put("message", "頭像上傳失敗,請檢查圖片!");

resultMap.put("status", 400);

return resultMap;

}

}

前端的UI

開始準備測試

開始編寫拋出異常()

主要是出現問題是SQL查詢read-only

Cause: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not

blog.csdn.net/u01141052()

query*的配置成了read-only

<tx:advice id="txAdvice" transaction-manager="transactionManager">

<tx:attributes>

<tx:method name="publish*" />

<tx:method name="save*" />

<tx:method name="add*" />

<tx:method name="update*" read-only="false" />

<tx:method name="insert*" />

<tx:method name="create*" />

<tx:method name="del*" />

<tx:method name="load*" />

<tx:method name="init*" />

<tx:method name="*" /> <!--read-only="true"-->

</tx:attributes>

</tx:advice>

最後測試成功(文件上傳成功)

查看雲端

查看上傳的圖片

資料庫裡面儲存成功

前面是整合七牛雲的

---------------------------------------------------------------------------------------------------------------------------

接下來是整合(阿里雲的OSS API)

開始的步驟是一樣的

依賴前面已經弄過了

直接查看官方的API的文檔,複寫自己需要的方法

//阿里流上傳

public String Aliupload(InputStream inputStream) {

String endpoint = " XXXXXX ";

String endpoint1 = " XXXXXX ";

// 雲賬號AccessKey有所有API訪問許可權,建議遵循阿里雲安全最佳實踐,創建並使用RAM子賬號進行API訪問或日常運維,請登錄 ram.console.aliyun.com 創建。

String accessKeyId = " XXXXXX ";

String accessKeySecret = " XXXXXX ";

String bucketname=" XXXXXX ";

String IMG_UPLOAD_PATH = "";

String name_file = CharacterUtils.getRandomString2(8);//隨機數

String key = IMG_UPLOAD_PATH +name_file+"fv_.png";

try {

OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

/*InputStream inputStream = new FileInputStream(filePath);*/

ossClient.putObject(bucketname, key, inputStream/*new File(filePath)*/);

inputStream.close();

ossClient.shutdown();

} catch (OSSException oe) {

System.out.println("Caught an upLoad OSSException," + "the error code is " + oe.getErrorCode() + "," + "reason is " + oe.getMessage());

}catch (IOException e) {

System.out.println("File upload problem,throw a IOException");

}

return endpoint1+key;

}

還有流的轉換

public class MultFileToIoFile {

private final static Logger LOGGER = LoggerFactory.getLogger(MultFileToIoFile.class);

public static InputStream multipartToInputStream(MultipartFile multipartFile){

CommonsMultipartFile cf = (CommonsMultipartFile) multipartFile;

DiskFileItem diskFileItem = (DiskFileItem) cf.getFileItem();

try{

InputStream inputStream = diskFileItem.getInputStream();

return inputStream;

}catch (IOException e){

e.printStackTrace();

LOGGER.info("文件流傳輸失敗");

}

return null;

}

}

接下來就是control

//文件上傳

@RequestMapping(value = "upload",method=RequestMethod.POST)

@ResponseBody

public Map<String,Object> upload(HttpServletRequest request, MultipartFile file)throws Exception {

resultMap.put("status", 400);

if (checkSuffix(file.getOriginalFilename())) {

/*QIniuUpload qIniuUpload = new QIniuUpload();

String head = qIniuUpload.uploadImg2QiNiu(file);

System.out.println(head);*/

AliUpload aliUpload =new AliUpload();

String head = aliUpload.updateFile(file);

System.out.println(head+"1111111111111111111111111111111111111111111");

UUser user = new UUser(head,TokenManager.getToken().getId());

userService.upload(user);

System.out.println("gooood");

resultMap.put("message", "頭像上傳成功!");

resultMap.put("status", 200);

return resultMap;

}else{

resultMap.put("message", "頭像上傳失敗,請檢查圖片!");

resultMap.put("status", 400);

return resultMap;

}

}

但是中間出現一個問題

java.lang.NoClassDefFoundError: org/apache/http/ssl/TrustStrategy

<dependency>

<groupId>org.apache.httpcomponents</groupId>

<artifactId>httpclient-cache</artifactId>

<version>4.4.1</version>

</dependency>

<dependency>

<groupId>org.apache.httpcomponents</groupId>

<artifactId>httpmime</artifactId>

<version>4.4.1</version>

</dependency>

主要是一類httpclient的版本衝突

解決方法:

方法一:刪除其他版本的httpclient,如下截圖,httpclient版本除了阿里雲sdk依賴的沒有其他版本。

方法二:添加阿里雲sdk排除httpclient依賴:

我把全部的改成httpclient-4.4.1(接下來就可以運行了)

上傳文件

查看阿里雲的圖片情況

數據裡面存儲的數據信息

今天在項目中整合:圖片上傳功能,把阿里雲和七牛雲的功能全部整合到項目中去了,自己也再次熟悉了文件上傳和API介面,不同的廠商對應的不同的API的介面的API使用不同

今天遇到的困難:第一個就是七牛的開始文件上傳的read-only還有阿里雲的httpclient的依賴衝突,最後都完美的解決了

明天的計劃:繼續完成其他的shiro設計還有更新數據表的實現


推薦閱讀:

Redis持久化:AOF模式
Redis學習筆記(一)
【RabbitMQ學習記錄】- 消息隊列存儲機制源碼分析
redis學習系列(四)——redis高級應用(集群搭建、集群分區原理、集群操作)
9個提升逼格的Redis命令

TAG:SpringMVC | MyBatis | Redis |