关键词搜索

源码搜索 ×
×

Java 并发写文件加锁处理(阿里云OSS HTTP文件迁移)

发布2020-06-11浏览992次

详情内容

目录

文件追加的三种方式

OSS HTTP list.txt输出方式


阿里云OSS文件资源行数据迁移:

https://help.aliyun.com/document_detail/95134.html?spm=a2c4g.11186623.6.555.32755641l0V8YD

文件追加的三种方式

文件多线程写的情况下需要加锁处理: 提供三种方式写入文件内容。

  1. /**
  2. * 追加写入内容到目标文件
  3. *
  4. * @param dwCode
  5. * @param rowsContent
  6. * @param destFile
  7. */
  8. private synchronized void writeIntoFile(String dwCode,String rowsContent,File destFile){
  9. log.info("writeIntoFile {} dts data......", dwCode);
  10. OutputStream os = null;
  11. try {
  12. if (!destFile.exists()) {
  13. destFile.createNewFile();
  14. }
  15. // 追加写入内容
  16. os = new FileOutputStream(destFile,true);
  17. os.write(rowsContent.getBytes());
  18. log.info("writeIntoFile {} dts data......success!", dwCode);
  19. } catch (FileNotFoundException e) {
  20. log.info("writeIntoFile {} dts data......FileNotFoundException!", dwCode);
  21. e.printStackTrace();
  22. } catch (IOException e) {
  23. log.info("writeIntoFile {} dts data......IOException!", dwCode);
  24. e.printStackTrace();
  25. } finally {
  26. if (null != os) {
  27. try {
  28. os.close();
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. }
  34. }
  35. /**
  36. * 追加写入内容到目标文件
  37. *
  38. * @param dwCode
  39. * @param rowsContent
  40. * @param destFile
  41. */
  42. private void writeIntoFileByBufferedWriter(String dwCode,String rowsContent,File destFile){
  43. log.info("writeIntoFileByBufferedWriter {} dts data......", dwCode);
  44. BufferedWriter out = null;
  45. try {
  46. if (!destFile.exists()) {
  47. destFile.createNewFile();
  48. }
  49. // 追加写入内容
  50. out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFile, true)));
  51. out.write(rowsContent);
  52. log.info("writeIntoFileByBufferedWriter {} dts data......success!", dwCode);
  53. } catch (FileNotFoundException e) {
  54. log.info("writeIntoFileByBufferedWriter {} dts data......FileNotFoundException!", dwCode);
  55. e.printStackTrace();
  56. } catch (IOException e) {
  57. log.info("writeIntoFileByBufferedWriter {} dts data......IOException!", dwCode);
  58. e.printStackTrace();
  59. } finally {
  60. if(null != out){
  61. try {
  62. out.close();
  63. } catch (IOException e) {
  64. e.printStackTrace();
  65. }
  66. }
  67. }
  68. }
  69. /**
  70. * 追加写入内容到目标文件
  71. *
  72. * @param dwCode
  73. * @param rowsContent
  74. * @param destFile
  75. */
  76. private synchronized void writeIntoFileByFileWriter(String dwCode,String rowsContent,File destFile){
  77. log.info("writeIntoFileByAppend {} dts data......", dwCode);
  78. OutputStream os = null;
  79. try {
  80. if (!destFile.exists()) {
  81. destFile.createNewFile();
  82. }
  83. // 追加写入内容
  84. FileWriter fileWriter =new FileWriter(destFile,true);
  85. fileWriter.write(rowsContent);
  86. log.info("writeIntoFileByAppend {} dts data......success!", dwCode);
  87. } catch (FileNotFoundException e) {
  88. log.info("writeIntoFileByAppend {} dts data......FileNotFoundException!", dwCode);
  89. e.printStackTrace();
  90. } catch (IOException e) {
  91. log.info("writeIntoFileByAppend {} dts data......IOException!", dwCode);
  92. e.printStackTrace();
  93. } finally {
  94. if (null != os) {
  95. try {
  96. os.close();
  97. } catch (IOException e) {
  98. e.printStackTrace();
  99. }
  100. }
  101. }
  102. }

OSS HTTP list.txt输出方式

pom.xml

  1. <dependency>
  2. <groupId>com.aliyun.oss</groupId>
  3. <artifactId>aliyun-sdk-oss</artifactId>
  4. <version>3.8.0</version>
  5. </dependency>

Java 代码

  1. package com.aliyun.service.oss.tds;
  2. import com.aliyun.service.oss.service.BaseService;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.apache.commons.lang3.StringUtils;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.stereotype.Service;
  8. import java.io.*;
  9. import java.util.List;
  10. import java.util.Map;
  11. /**
  12. * @Copyright: 2019-2021
  13. * @FileName: JilinFileService.java
  14. * @Author: PJL
  15. * @Date: 2020/6/10 17:02
  16. * @Description: **文件服务
  17. */
  18. @Slf4j
  19. @Service
  20. public class FileService extends BaseService {
  21. @Value("${system.http-base-url}")
  22. String httpBaseUrl;
  23. @Value("${system.local-save-base-path}")
  24. String localSaveBasePath;
  25. @Autowired
  26. FujianService fujianService;
  27. /**
  28. * 解析文件路径
  29. *
  30. * @param map
  31. * @param sb
  32. * @param fieldName
  33. */
  34. private String parseFilePath(Map<String, Object> map,StringBuffer sb,String fieldName){
  35. Object value = map.get(fieldName);
  36. if(null == value){
  37. return "";// 无值情况不予处理
  38. }
  39. String file = value.toString();
  40. String httpUrl = new StringBuffer(httpBaseUrl).append("/").append(file).toString();
  41. //String destUrl = new StringBuffer(dwCode).append("/").append(file).toString();
  42. String destUrl = new StringBuffer(file).toString();
  43. String rowContent = new StringBuffer(httpUrl).append("\t").append(destUrl).append("\n").toString();
  44. sb.append(rowContent);
  45. return destUrl;
  46. }
  47. /**
  48. * 解析并封装文件路径集合字符串内容
  49. *
  50. * @param dwCode
  51. * @param dataList
  52. */
  53. private String parseDataListContent(String dwCode, List<Map<String, Object>> dataList) {
  54. StringBuffer sb = new StringBuffer();
  55. if (null != dataList && dataList.size() > 0) {
  56. for (Map<String, Object> map : dataList) {
  57. // 原文件解析
  58. String destUrl = this.parseFilePath(map,sb,"FJ_LJ");
  59. // 缩略图解析
  60. if(StringUtils.isNotEmpty(destUrl) && !destUrl.contains(".mp4")){
  61. this.parseFilePath(map,sb,"FJ_LJ_COMPRES");
  62. }
  63. }
  64. }
  65. return sb.toString();
  66. }
  67. /**
  68. * 创建路径
  69. *
  70. * @param filePath
  71. */
  72. private void createDir(String filePath) {
  73. File dwDirFile = new File(filePath);
  74. if (!dwDirFile.exists()) {
  75. dwDirFile.mkdir();
  76. }
  77. }
  78. /**
  79. * 追加写入内容到目标文件
  80. *
  81. * @param dwCode
  82. * @param rowsContent
  83. * @param destFile
  84. */
  85. private synchronized void writeIntoFile(String dwCode,String rowsContent,File destFile){
  86. log.info("writeIntoFile {} dts data......", dwCode);
  87. OutputStream os = null;
  88. try {
  89. if (!destFile.exists()) {
  90. destFile.createNewFile();
  91. }
  92. // 追加写入内容
  93. os = new FileOutputStream(destFile,true);
  94. os.write(rowsContent.getBytes());
  95. log.info("writeIntoFile {} dts data......success!", dwCode);
  96. } catch (FileNotFoundException e) {
  97. log.info("writeIntoFile {} dts data......FileNotFoundException!", dwCode);
  98. e.printStackTrace();
  99. } catch (IOException e) {
  100. log.info("writeIntoFile {} dts data......IOException!", dwCode);
  101. e.printStackTrace();
  102. } finally {
  103. if (null != os) {
  104. try {
  105. os.close();
  106. } catch (IOException e) {
  107. e.printStackTrace();
  108. }
  109. }
  110. }
  111. }
  112. /**
  113. * 追加写入内容到目标文件
  114. *
  115. * @param dwCode
  116. * @param rowsContent
  117. * @param destFile
  118. */
  119. private void writeIntoFileByBufferedWriter(String dwCode,String rowsContent,File destFile){
  120. log.info("writeIntoFileByBufferedWriter {} dts data......", dwCode);
  121. BufferedWriter out = null;
  122. try {
  123. if (!destFile.exists()) {
  124. destFile.createNewFile();
  125. }
  126. // 追加写入内容
  127. out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFile, true)));
  128. out.write(rowsContent);
  129. log.info("writeIntoFileByBufferedWriter {} dts data......success!", dwCode);
  130. } catch (FileNotFoundException e) {
  131. log.info("writeIntoFileByBufferedWriter {} dts data......FileNotFoundException!", dwCode);
  132. e.printStackTrace();
  133. } catch (IOException e) {
  134. log.info("writeIntoFileByBufferedWriter {} dts data......IOException!", dwCode);
  135. e.printStackTrace();
  136. } finally {
  137. if(null != out){
  138. try {
  139. out.close();
  140. } catch (IOException e) {
  141. e.printStackTrace();
  142. }
  143. }
  144. }
  145. }
  146. /**
  147. * 追加写入内容到目标文件
  148. *
  149. * @param dwCode
  150. * @param rowsContent
  151. * @param destFile
  152. */
  153. private synchronized void writeIntoFileByFileWriter(String dwCode,String rowsContent,File destFile){
  154. log.info("writeIntoFileByAppend {} dts data......", dwCode);
  155. OutputStream os = null;
  156. try {
  157. if (!destFile.exists()) {
  158. destFile.createNewFile();
  159. }
  160. // 追加写入内容
  161. FileWriter fileWriter =new FileWriter(destFile,true);
  162. fileWriter.write(rowsContent);
  163. log.info("writeIntoFileByAppend {} dts data......success!", dwCode);
  164. } catch (FileNotFoundException e) {
  165. log.info("writeIntoFileByAppend {} dts data......FileNotFoundException!", dwCode);
  166. e.printStackTrace();
  167. } catch (IOException e) {
  168. log.info("writeIntoFileByAppend {} dts data......IOException!", dwCode);
  169. e.printStackTrace();
  170. } finally {
  171. if (null != os) {
  172. try {
  173. os.close();
  174. } catch (IOException e) {
  175. e.printStackTrace();
  176. }
  177. }
  178. }
  179. }
  180. /**
  181. * 写入同一个文件
  182. *
  183. * @param dwCode
  184. * @param rowsContent
  185. */
  186. private void saveToSingleFile(String dwCode, String rowsContent) {
  187. log.info("save saveToSingleFile {} dts data......", dwCode);
  188. String filePath = new StringBuffer(localSaveBasePath).append("//").append("list.txt").toString();
  189. File destFile = new File(filePath);
  190. this.writeIntoFile(dwCode,rowsContent,destFile);
  191. }
  192. /**
  193. * 保存单位附件文件
  194. *
  195. * @param type
  196. * @param dwCode
  197. * @param rowsContent
  198. */
  199. private synchronized void saveToMultiFile(String type, String dwCode, String rowsContent) {
  200. if (StringUtils.isNotEmpty(rowsContent)) {
  201. /创建路径//S/
  202. String dwDir = new StringBuffer(localSaveBasePath).append("//").append(type).toString();
  203. this.createDir(dwDir);
  204. String dwDir2 = new StringBuffer(dwDir).append("//").append(dwCode).toString();
  205. this.createDir(dwDir2);
  206. /创建路径//E/
  207. String destFilePath = new StringBuffer(dwDir2).append("//").append("list.txt").toString();
  208. File destFile = new File(destFilePath);
  209. this.writeIntoFile(dwCode,rowsContent,destFile);
  210. }
  211. }
  212. /**
  213. * 保存病疫监测事件附件
  214. *
  215. */
  216. private void saveByjcEventFujianAll(boolean writeSingleFile) {
  217. List<String> list = fujianService.getExportCorpList();
  218. log.info("ByjcEvent getExportCorpList size = {}", list.size());
  219. for (String dwCode : list) {
  220. List<Map<String, Object>> dataList = fujianService.getEventFujian(FujianService.XH_BYJC_FJ_TB, dwCode);
  221. // 解析并保存病疫监测附件数据
  222. String rowsContent = this.parseDataListContent(dwCode, dataList);
  223. // 保存迁移文件内容
  224. if(writeSingleFile){
  225. this.saveToSingleFile(dwCode, rowsContent);
  226. }else{
  227. this.saveToMultiFile(FujianService.XH_BYJC_FJ_TB, dwCode, rowsContent);
  228. }
  229. }
  230. log.info("ByjcEvent getExportCorpList size = {} finished!", list.size());
  231. }
  232. /**
  233. * 保存公共事件附件
  234. *
  235. */
  236. private void saveShijianEventFujianAll(boolean writeSingleFile) {
  237. List<String> list = fujianService.getExportCorpList();
  238. log.info("ShijianEvent getExportCorpList size = {}", list.size());
  239. for (String dwCode : list) {
  240. List<Map<String, Object>> dataList = fujianService.getEventFujian(FujianService.XH_SHIJIAN_FJ_TB, dwCode);
  241. // 解析并保存病疫监测附件数据
  242. String rowsContent = this.parseDataListContent(dwCode, dataList);
  243. // 保存迁移文件内容
  244. if(writeSingleFile){
  245. this.saveToSingleFile(dwCode, rowsContent);
  246. }else{
  247. this.saveToMultiFile(FujianService.XH_SHIJIAN_FJ_TB, dwCode, rowsContent);
  248. }
  249. }
  250. log.info("ShijianEvent getExportCorpList size = {} finished!", list.size());
  251. }
  252. /**
  253. * 开启线程处理附件
  254. *
  255. */
  256. public void startTDSStoreThreads(boolean writeSingleFile) {
  257. log.info("start threads.....");
  258. new Thread(() -> {
  259. this.saveByjcEventFujianAll(writeSingleFile);
  260. }).start();
  261. new Thread(() -> {
  262. this.saveShijianEventFujianAll(writeSingleFile);
  263. }).start();
  264. log.info("start threads.....success!");
  265. }
  266. }

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载