SpringCloud实现IOS应用安装包上传、plist文件生成和浏览器下载

(一)环境及配置文件设置

(1)thymeleaf配置

pom.xml文件中引入thymeleaf依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

springboot配置文件设置thymeleaf

#####################thymeleaf#####################
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false

(2)文件大小限制设置

网关使用的是Zuul(springboot版本2.0以下)
配置文件设置为:

#####################FILE############################
spring.http.multipart.enabled=true
spring.http.multipart.file-size-threshold=0
spring.http.multipart.max-request-size=1024MB
spring.http.multipart.max-file-size=300MB

文件上传工程(springboot版本2.0以上)
配置文件设置为:

spring.servlet.multipart.max-request-size=1024MB
spring.servlet.multipart.max-file-size=300MB
spring.servlet.multipart.enabled=true
spring.servlet.multipart.file-size-threshold=0

(3)上传文件默认访问路径设置

##############################StaticResource###################
web.upload-path=/my/www/download
spring.mvc.static-path-pattern=/**
spring.resources.static-locations=classpath:/static/,file:${web.upload-path}

设置上传后的文件外部地址默认访问目录(/my/www/download)
例如:该目录下存在一个test.jpg,浏览器直接访问:https://host/fileupload/test.jpg 即可。fileupload为个人工程服务名称

五级标题
六级标题

(二)文件上传

(1)前端html部分

html部分代码

<div style="text-align:center">
	<h1 th:inlines="text">安装包上传</h1>
	<form id="iphone" action="uploadIphone" method="post" enctype="multipart/form-data" onsubmit="return checkIphone(this);">
		<table style="width:40%;margin:auto">
			<p>IOS客户端:</p>
			<tr>
				<td align="right" width="40%">业务系统英文简称:</td>
				<td align="left" width="60%"><input type="text" id="system" name="system" style="width:333px"></td>
	        </tr>
	        <tr>
				<td align="right" width="40%">安装包中文名称:</td>
				<td align="left" width="60%"><input type="text" id="appName" name="appName" style="width:333px"></td>
	        </tr>
	        <tr>
				<td align="right" width="40%">安装包ID(Bundle ID)</td>
				<td align="left" width="60%"><input type="text" id="bundleId" name="bundleId" style="width:333px"></td>
	        </tr>
	        <tr>
				<td align="right" width="40%">版本号:</td>
				<td align="left" width="60%"><input type="text" id="versionCode" name="versionCode" style="width:333px"></td>
	        </tr>
			<tr>
				<td align="right" width="40%">点击选择文件:</td>
	        	<td align="left" width="60%"><input type="file" name="fileName" style="width:304px"/></td>
	        </tr>
	        <tr>
	        	<td align="right" width="40%"></td>
	            <td align="right" width="60%"><input type="submit" value="文件上传"/></td>
	        </tr>
	    </table>
	</form>
</div>

JS代码

<script type="text/javascript">
	function checkAndroid(obj){
	  	if(obj.system.value == ''){
	    	alert("业务系统英文名称不能为空!");
	    	return false;
	  	}
	  	if(obj.appName.value == ''){
	    	alert("安装包中文名称不能为空!");
	    	return false;
	  	}
	  	var strFileName = obj.fileName.value;
		if(strFileName==""){
    		alert("请选择要上传的文件");
    		return false;
  		}
  		var strtype = strFileName.substring(strFileName.length-3,strFileName.length);
  		strtype = strtype.toLowerCase();
	  	if(strtype=="apk"){
    		return true;
		}else{
    		alert("文件类型错误,请上传apk安装包!");
    		android.FileName.focus();
    		return false;
  		}
    	return true;
  	}
</script>

(2)后台Java部分

Java代码
controller部分

    /**
     * 实现文件上传-苹果端
	 * @throws Exception 
     * */
    @RequestMapping("/uploadIphone")
    @ResponseBody 
    public String uploadIphone(@RequestParam("fileName") MultipartFile file,HttpServletRequest request) throws Exception{
    	logger.info("IOS安装包上传 ");
    	String system = request.getParameter("system");
    	String appName = request.getParameter("appName");
    	String versionCode = request.getParameter("versionCode");
    	String bundleId = request.getParameter("bundleId");
    	logger.info("业务系统: "+system+"--"+appName+"--"+versionCode);
    	return getFileService().fileUploadForIphone(file,system,appName,bundleId,versionCode);
    }

service部分

	/**
	 * 单文件上传 
	 * @throws Exception 
	 */
	public String fileUploadForIphone(MultipartFile file, String system, String appName, String bundleId, String versionCode) throws Exception{
		if(file.isEmpty()){
            return "上传失败,文件为空!";
        }
		String result = "";
        try {
        	String time = String.valueOf(System.currentTimeMillis()/1000);
        	//测试MultipartFile接口的各个方法
        	logger.info("[文件类型ContentType] - "+file.getContentType());
        	logger.info("[文件组件名称Name] - "+file.getName());
        	logger.info("[文件原名称OriginalFileName] - "+file.getOriginalFilename());
        	logger.info("[文件大小] - "+file.getSize());
        	String fileName = file.getOriginalFilename();
        	if(!fileName.endsWith(".ipa")){
        		return "文件类型错误,请上传ipa安装包!";
        	}
	        //文件路径
        	String filePath = fileConfig.getIphone()+system;
        	String uploadPath = filePath +"/" + time;
	        logger.info("安装包上传路径"+uploadPath);
	        File f = new File(uploadPath);
	        //如果不存在该路径就创建
	        if (!f.exists()) {
	            f.mkdirs();
	        }
	        //上传保存安装包
	        logger.info("安装包上传...");
	        File dest = new File(uploadPath +"/"+ fileName);
//	        if(!dest.getParentFile().exists()){ //判断文件父目录是否存在
//                dest.getParentFile().mkdir();
//            }
            file.transferTo(dest);
            //生成plist
            String fileUrl = fileConfig.getHostUrl()+"/secondgetway/appupload-service/iphone/"+system+"/"+time+"/"+fileName;
            logger.info("fileUrl="+fileUrl);
            boolean flag = createPlist(appName,filePath,bundleId,fileUrl,versionCode);
            if(flag){
            	result = fileConfig.getHostUrl()+"/secondgetway/appupload-service/iphone/"+system+"/iphone.plist";
            }
        } catch (Exception e) {
        	logger.error(e.getMessage(), e);
        	return "安装包上传失败!"+e;
        }
        return "<p>安装包上传成功!</p>"
        		+ "<p>plist文件地址:</p>"
        		+ "<p>"+result+"</p>";
	}

生成plist文件

	public boolean createPlist(String title, String plistPath, String bundleId, String fileUrl, String versionCode) throws Exception {
		logger.info("开始创建plist文件");
        boolean success = true;
        File file = new File(plistPath+"/iphone.plist");
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                logger.error("创建plist文件目录异常", e);
            }
        }
        String plist = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                + "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
                + "<plist version=\"1.0\">\n" 
                + "<dict>\n"
                + "<key>items</key>\n"
                + "<array>\n"
                + "<dict>\n"
                + "<key>assets</key>\n"
                + "<array>\n"
                + "<dict>\n"
                + "<key>kind</key>\n"
                + "<string>software-package</string>\n"
                + "<key>url</key>\n"
                // 之前所上传的ipa文件路径(必须是https,否则无法下载!)
                + "<string>" + fileUrl + "</string>\n"
                + "</dict>\n"
                + "</array>\n"
                + "<key>metadata</key>\n"
                + "<dict>\n"
                + "<key>bundle-identifier</key>\n"
                // 这个是开发者账号用户名,也可以为空,为空安装时看不到图标,完成之后可以看到
                + "<string>" + bundleId + "</string>\n"
                + "<key>bundle-version</key>\n"
                // 版本号
                + "<string>"+ versionCode +"</string>\n"
                + "<key>kind</key>\n"
                + "<string>software</string>\n"
                + "<key>subtitle</key>\n"
                + "<string>下载</string>\n"
                + "<key>title</key>\n"
                // 一定要有title,否则无法正常下载
                + "<string>"+ title +"</string>\n"
                + "</dict>\n"
                + "</dict>\n"
                + "</array>\n"
                + "</dict>\n"
                + "</plist>";
        try {
            FileOutputStream output = new FileOutputStream(file);
            OutputStreamWriter writer;
            writer = new OutputStreamWriter(output, "UTF-8");
            writer.write(plist);
            writer.close();
            output.close();
        } catch (Exception e) {
            logger.error("创建plist文件异常", e);
            return false;
        }
        logger.info("成功创建plist文件");
        return success;
    }

(三)实现效果

上传页面
在这里插入图片描述
上传成功提示:
在这里插入图片描述
查看服务器安装包及plist文件
在这里插入图片描述
在这里插入图片描述
测试下载:
将plist文件中的url安装包部分在浏览器打开,可将安装包下载到本地;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值