commons-fileupload文件的上传和加密保存,保存其他上传参数

现在pox.xml中添加使用的jar包注入
   
   
  1. <dependency>
  2. <groupId>commons-fileupload</groupId>
  3. <artifactId>commons-fileupload</artifactId>
  4. <version>1.3.2</version>
  5. </dependency>
前台页面form上传的时候添加为 multipart/form-data
   
   
  1. <form action="http://192.168.1.155:8092/spkupload" method="post" enctype="multipart/form-data">
  2. <fieldset>
  3. <label for="name">sdkName:</label>
  4. <input type="text" name="sdkName" placeholder="Enter your sdkName" />
  5. <label for="name">className:</label>
  6. <input type="text" name="className" placeholder="Enter your className" />
  7. <label for="name">method:</label>
  8. <input type="text" name="method" placeholder="Enter your method" />
  9. <label for="name">param:</label>
  10. <input type="text" name="param" placeholder="Enter your param" />
  11. <label for="name">delayTime:</label>
  12. <input type="text" name="delayTime" placeholder="Enter your delayTime" />
  13. <label for="name">File:</label>
  14. <input type="file" name="myFile" id="cusfile"/><br/>
  15. <input type="submit" value="Send message" />
  16. </fieldset>
  17. </form>
在controller中获取到上传的流
   
   
  1. @RequestMapping(value = "/spkupload", method = RequestMethod.POST)
  2. public void saveSDK(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
  3. Map<String, String> saveMap=new HashMap<String, String>();
  4. DiskFileItemFactory factory = new DiskFileItemFactory();
  5. //以byte为单位设定文件使用多少内存量后,将文件存入临时存储
  6. factory.setSizeThreshold(MEMORY_THRESHOLD);
  7. //设定临时文件的存储路径
  8. factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
  9. //ServletFileUpload 处理同意HTML文件中多文件上传的类,继承自FileUpload
  10. ServletFileUpload upload = new ServletFileUpload(factory);
  11. //设置允许上传文件的最大大小
  12. upload.setSizeMax(MAX_REQUEST_SIZE);
  13. //这是我这里从配置文件获取文件的存储路径 也可以固定死弄成d:/temp 路径
  14. String uploadPath=adSysConfigService.getConfByName("liveSDKActualURL");
  15. //String uploadPath = "d:/temp";
  16. System.out.println(uploadPath);
  17. //根据存储路径生成文件夹
  18. File uploadDir = new File(uploadPath);
  19. //判断文件路径是否存在若是不存在创建文件夹
  20. if(!uploadDir.exists()){
  21. uploadDir.mkdir();
  22. }
  23. try{
  24. List<FileItem> formItems = upload.parseRequest(request);
  25. if(formItems!=null && formItems.size()>0){
  26. //循环遍历items
  27. for(FileItem item:formItems){
  28. //判断item是否为文件
  29. if(!item.isFormField()){
  30. //我本地的业务要求上传的字段中有一个文件名字 最终我生成了这个名字的apk
  31. String fileName="s";
  32. for(FileItem item2:formItems){
  33. String itemName=item2.getFieldName();
  34. if (itemName.equalsIgnoreCase("sdkName")) {
  35. fileName=item2.getString();
  36. }
  37. }
  38. //String fileName = new File(item.getName()).getName();
  39. //此处我写了两个中间文件 一个是临时的文件一个是最终的文件 因为我需要对上传的文件进行加密
  40. String filePath1 = uploadPath + File.separator + fileName+".txt";
  41. String filePath = uploadPath + File.separator + fileName+".apk";
  42. File delete2=new File(filePath);
  43. if (delete2.isFile()) {
  44. boolean r2=delete2.delete();
  45. }
  46. System.out.println("filePath" + filePath);
  47. File storeFile = new File(filePath1);
  48. //写入文件
  49. item.write(storeFile);
  50. FileInputStream fis= new FileInputStream(filePath1); 
  51. //获取了一下文件的md5,根据自己的业务可以去掉这个功能
  52. String md5 = DigestUtils.md5Hex(IOUtils.toByteArray(fis));
  53. IOUtils.closeQuietly(fis);
  54. System.out.println("MD5:"+md5);
  55. saveMap.put("md5", md5);
  56. //对文件进行加密
  57. encryptFile(filePath1,filePath);
  58. File storeFile2=new File(filePath1);
  59. if (storeFile2.exists()) { // 不存在返回 false
  60. boolean result = storeFile2.delete();
  61. System.out.println(result);
  62. }
  63. request.setAttribute("message", "Upload has been done successfully!");
  64. }
  65. //对于不是文件的其他参数进行获取
  66. else {
  67. //如果包含中文应写为:(转为UTF-8编码)
  68. String key =item.getFieldName();
  69. String value=item.getString();
  70. saveMap.put(key, value);
  71. }
  72. }
  73. //保存文件的其他参数信息到数据库
  74. saveSdkInfo(saveMap);
  75. }
  76. }catch (Exception ex){
  77. request.setAttribute("message", "There was an error: " + ex.getMessage());
  78. }
在加密文件的时候对文件进行了二进制的转换
   
   
  1. public static byte[] File2byte(String filePath)
  2. {
  3. byte[] buffer = null;
  4. try
  5. {
  6. File file=new File(filePath);
  7. FileInputStream fis = new FileInputStream(file);
  8. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  9. byte[] b = new byte[1024];
  10. int n;
  11. while ((n = fis.read(b)) != -1)
  12. {
  13. bos.write(b, 0, n);
  14. }
  15. if (fis != null) {
  16. fis.close();
  17. }
  18. if (bos!=null) {
  19. bos.close();
  20. }
  21. buffer = bos.toByteArray();
  22. }
  23. catch (FileNotFoundException e)
  24. {
  25. e.printStackTrace();
  26. }
  27. catch (IOException e)
  28. {
  29. e.printStackTrace();
  30. }
  31. return buffer;
  32. }
最终将二进制的字符转换成文件
   
   
  1. public static void byte2File(byte[] buf, String filePath)
  2. {
  3. BufferedOutputStream bos = null;
  4. FileOutputStream fos = null;
  5. File file = null;
  6. try
  7. {
  8. File dir = new File(filePath);
  9. if (!dir.exists() && dir.isDirectory())
  10. {
  11. dir.mkdirs();
  12. }
  13. file = new File(filePath);
  14. fos = new FileOutputStream(file);
  15. bos = new BufferedOutputStream(fos);
  16. bos.write(buf);
  17. }
  18. catch (Exception e)
  19. {
  20. e.printStackTrace();
  21. }
  22. finally
  23. {
  24. if (bos != null)
  25. {
  26. try
  27. {
  28. bos.close();
  29. }
  30. catch (IOException e)
  31. {
  32. e.printStackTrace();
  33. }
  34. }
  35. if (fos != null)
  36. {
  37. try
  38. {
  39. fos.close();
  40. }
  41. catch (IOException e)
  42. {
  43. e.printStackTrace();
  44. }
  45. }
  46. }
  47. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值