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
网关使用的是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
##############################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为个人工程服务名称
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>
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 "安装包上传成功!
"
+ "plist文件地址:
"
+ ""
+result+"";
}
生成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 = "\n"
+ "\n"
+ "\n"
+ "\n"
+ "items \n"
+ "\n"
+ "\n"
+ "assets \n"
+ "\n"
+ "\n"
+ "kind \n"
+ "software-package \n"
+ "url \n"
// 之前所上传的ipa文件路径(必须是https,否则无法下载!)
+ "" + fileUrl + "\n"
+ "\n"
+ "\n"
+ "metadata \n"
+ "\n"
+ "bundle-identifier \n"
// 这个是开发者账号用户名,也可以为空,为空安装时看不到图标,完成之后可以看到
+ "" + bundleId + "\n"
+ "bundle-version \n"
// 版本号
+ "" + versionCode +"\n"
+ "kind \n"
+ "software \n"
+ "subtitle \n"
+ "下载 \n"
+ "title \n"
// 一定要有title,否则无法正常下载
+ "" + title +"\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "";
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安装包部分在浏览器打开,可将安装包下载到本地;