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

xiaoxiao2021-02-27  331

现在pox.xml中添加使用的jar包注入 <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency> 前台页面form上传的时候添加为 multipart/form-data <form action="http://192.168.1.155:8092/spkupload" method="post" enctype="multipart/form-data"> <fieldset> <label for="name">sdkName:</label> <input type="text" name="sdkName" placeholder="Enter your sdkName" /> <label for="name">className:</label> <input type="text" name="className" placeholder="Enter your className" /> <label for="name">method:</label> <input type="text" name="method" placeholder="Enter your method" /> <label for="name">param:</label> <input type="text" name="param" placeholder="Enter your param" /> <label for="name">delayTime:</label> <input type="text" name="delayTime" placeholder="Enter your delayTime" /> <label for="name">File:</label> <input type="file" name="myFile" id="cusfile"/><br/> <input type="submit" value="Send message" /> </fieldset> </form> 在controller中获取到上传的流 @RequestMapping(value = "/spkupload", method = RequestMethod.POST) public void saveSDK(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { Map<String, String> saveMap=new HashMap<String, String>(); DiskFileItemFactory factory = new DiskFileItemFactory(); //以byte为单位设定文件使用多少内存量后,将文件存入临时存储 factory.setSizeThreshold(MEMORY_THRESHOLD); //设定临时文件的存储路径 factory.setRepository(new File(System.getProperty("java.io.tmpdir"))); //ServletFileUpload 处理同意HTML文件中多文件上传的类,继承自FileUpload ServletFileUpload upload = new ServletFileUpload(factory); //设置允许上传文件的最大大小 upload.setSizeMax(MAX_REQUEST_SIZE);//这是我这里从配置文件获取文件的存储路径 也可以固定死弄成d:/temp 路径 String uploadPath=adSysConfigService.getConfByName("liveSDKActualURL"); //String uploadPath = "d:/temp"; System.out.println(uploadPath); //根据存储路径生成文件夹 File uploadDir = new File(uploadPath); //判断文件路径是否存在若是不存在创建文件夹 if(!uploadDir.exists()){ uploadDir.mkdir(); } try{ List<FileItem> formItems = upload.parseRequest(request); if(formItems!=null && formItems.size()>0){ //循环遍历items for(FileItem item:formItems){ //判断item是否为文件 if(!item.isFormField()){ //我本地的业务要求上传的字段中有一个文件名字 最终我生成了这个名字的apk String fileName="s"; for(FileItem item2:formItems){ String itemName=item2.getFieldName(); if (itemName.equalsIgnoreCase("sdkName")) { fileName=item2.getString(); } } //String fileName = new File(item.getName()).getName(); //此处我写了两个中间文件 一个是临时的文件一个是最终的文件 因为我需要对上传的文件进行加密 String filePath1 = uploadPath + File.separator + fileName+".txt"; String filePath = uploadPath + File.separator + fileName+".apk"; File delete2=new File(filePath); if (delete2.isFile()) { boolean r2=delete2.delete(); } System.out.println("filePath" + filePath); File storeFile = new File(filePath1); //写入文件 item.write(storeFile); FileInputStream fis= new FileInputStream(filePath1);  //获取了一下文件的md5,根据自己的业务可以去掉这个功能 String md5 = DigestUtils.md5Hex(IOUtils.toByteArray(fis)); IOUtils.closeQuietly(fis); System.out.println("MD5:"+md5); saveMap.put("md5", md5); //对文件进行加密 encryptFile(filePath1,filePath); File storeFile2=new File(filePath1); if (storeFile2.exists()) { // 不存在返回 false boolean result = storeFile2.delete(); System.out.println(result); } request.setAttribute("message", "Upload has been done successfully!"); } //对于不是文件的其他参数进行获取 else { //如果包含中文应写为:(转为UTF-8编码) String key =item.getFieldName(); String value=item.getString(); saveMap.put(key, value); } } //保存文件的其他参数信息到数据库 saveSdkInfo(saveMap); } }catch (Exception ex){ request.setAttribute("message", "There was an error: " + ex.getMessage()); } 在加密文件的时候对文件进行了二进制的转换 public static byte[] File2byte(String filePath) { byte[] buffer = null; try { File file=new File(filePath); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } if (fis != null) { fis.close(); } if (bos!=null) { bos.close(); } buffer = bos.toByteArray(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buffer; } 最终将二进制的字符转换成文件 public static void byte2File(byte[] buf, String filePath) { BufferedOutputStream bos = null; FileOutputStream fos = null; File file = null; try { File dir = new File(filePath); if (!dir.exists() && dir.isDirectory()) { dir.mkdirs(); } file = new File(filePath); fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); bos.write(buf); } catch (Exception e) { e.printStackTrace(); } finally { if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
转载请注明原文地址: https://www.6miu.com/read-2974.html

最新回复(0)