1、jsp中注意问题
//文件上传,注意form中要定义 enctype="multipart/form-data"
<form action="<%=basePath%>system/appversion/save.do" method="post"
id="form-appversion-edit-add" enctype="multipart/form-data" >
<div>
<label >文件:</label>
<input type="file" name="apkFile" id="apkFile"><button type="button" id="uploadButton" onclick="uploadApk()">上传</button>
</div>
<div >
<label >文件路径:</label>
<input type="text" class="input-text" value="${pd.appUrl }" name="appUrl" id="appUrl_edit" datatype="*" nullmsg="请选择文件" readonly="readonly">
</div>
<div>
<input id="appEdit_submitButton" class="btn btn-primary radius" type="submit" value=" 提交 ">
</div>
</form>
②js中ajax中提交文件
<script type="text/javascript">
//上传文件,这里是封装到form数据中传给后台。不是form的submit提交。
function uploadApk(){
var apkFile = $("#apkFile").val();
if(apkFile == ''){
layer.alert("请选择文件");
return;
}
var formData = new FormData($("#apkFile")[0].files[0]);
formData.append("apkFile",$("#apkFile")[0].files[0]);
$.ajax({
type:'POST',
url:'system/appversion/upload.do',
data:formData,
dataType:"json",
processData : false,
contentType : false,
success: function(data){
$("#appUrl_edit").val(data.appUrl);
$("#uploadButton").attr("disabled", true);
},
error: function(err){
layer.alert("服务器错误");
}
});
}
</script>
③后台上传的方法
/**
*app版本管理上传文件
*
* @param
* @throws Exception
*/
@ActionDesc(description="app版本管理上传文件",methodType="post")
@RequestMapping(value = "/upload")
@ResponseBody
public AppVersion upload(@RequestParam(required = false)MultipartFile apkFile) throws Exception {
String appUrl = "";
if (!apkFile.isEmpty()) {
// 文件原名称
String fileName = apkFile.getOriginalFilename();
String formatDay = DateUtil.getDays();
// 为了避免重复简单处理
String photoImagePath = PropertyUtil.readValue(Const.APK_PATH);
long currentTimeMillis = System.currentTimeMillis();
// 上传位置路径
String path0 = photoImagePath + "/" + formatDay + "/" + currentTimeMillis + "/" + fileName;
// 按照路径新建文件
File newFile = new File(path0);
File newFile1 = new File(photoImagePath + "/" + formatDay + "/" + currentTimeMillis);
if (!newFile1.exists()) {
newFile1.mkdirs();
}
// 复制
FileCopyUtils.copy(apkFile.getBytes(), newFile);
appUrl = "appversion/downloadNewApk.do?newApkUrl=" + path0;
}
AppVersion appVersion = new AppVersion();
appVersion.setAppUrl(appUrl);
return appVersion;
}
④后台下载方法
只要不是在项目中,即在其他目录下,那么就需要通过流的方式去读取文件,返回一个流。
/**
* 下载apk
*
* @param newApkUrl
* 最新的apk下载的URL
*
*/
@ActionDesc(description = "下载apk", methodType = "post")
@RequestMapping("/downloadNewApk")
@ResponseBody
public void downloadNewApk(String newApkUrl) throws Exception {
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getResponse();// 获取response
if (!Tools.isEmpty(newApkUrl)) {
// 文件路径(windows下是\\,linux下是//,都必须是绝对路径)
String path = PropertyUtil.readValue(Const.APK_PATH) + newApkUrl;
// java中用File类来表示一个文件
java.io.File newFile = new java.io.File(path);
// 测试这个文件路径是否存在(也就是这个文件是否存在)
if (!newFile.exists()) {
return;
}
// FileUtils.readFileToByteArray(File file)把一个文件转换成字节数组返回
byte[] data = FileUtils.readFileToByteArray(newFile);
String[] strArr = newApkUrl.split("/");
String fileName = strArr[strArr.length - 1];
response.reset();
// 设置文件的返回类型
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.addHeader("Content-Length", "" + data.length);
OutputStream outputStream = response.getOutputStream();
outputStream.write(data);
// java在使用流时,都会有一个缓冲区,按一种它认为比较高效的方法来发数据:
// 把要发的数据先放到缓冲区,缓冲区放满以后再一次性发过去,而不是分开一次一次地发.
// 而flush()表示强制将缓冲区中的数据发送出去,不必等到缓冲区满.
outputStream.flush();
outputStream.close();
} else {
return;
}
}
转载于:https://blog.51cto.com/jianboli/2045378