<div>
<div>
<legend class="search_legend" style="font-size: 14px"><strong>附件 </strong></legend>
<input ng-model="txtFoo" type="text" id="txtFoo" readonly style="width: 353px;">
<input class="btn btn-info" type="button" onclick="file.click()" value="选择文件">
<span style="color: red">(支持类型png、jpg、gif、zip、rar,单个文件最大5M,最多10个文件)</span>
<input type="file" id="file" multiple="multiple" name="files" style="visibility:hidden">
</div>
<div class="form-group">
<div class="col-sm-8 mgt15">
<div class="col-sm-7">
<table class="table table-striped" >
<tbody>
<tr ng-repeat="i in addParamsm.qaFileRelationList">
<th>{{i.filename}}</a></th>
<th style="width: 150px">
<button class='btn btn-primary btn-xs' type='button' ng-click='dowonload(i.id,i.filename)'><i class='fa fa-download'></i>
</button>
<span> </span>
<button class='btn btn-danger btn-xs' type='button' ng-click='removePic(i.id)'><i class='fa fa-trash-o'></i></button>
</th>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
$scope.dowonload = function (pid,filename) {
var fd = new FormData();
fd.append("id", pid);
$http({
method:'POST',
url : 'demo/download.do?id=' + pid,
data: fd,
responseType: 'blob',
}).success(function (response) {
downLoadFile(response,filename)
}).error(function () {
});
};
$scope.removePic = function (pid) {
$rootScope.ajax.post("demo/removeFileRelation.do?id=" + pid, {})
.then(function (result) {
//$scope.id=result.data;
if (result.code == "000000") { //成功
alert(result.msg);
$scope.getScriptById();
} else { //失败
alert(result.msg);
}
})
}
@RequestMapping(value = "/download")
public JsonResult download(HttpServletRequest request, HttpServletResponse response, Long id) {
FileDTO fileDTO= demoService.dowonloadFileRelation(id);
String realName = fileDTO.getFilename(); //文件在浏览器的显示位置
String osName = System.getProperty("os.name");
String downLoadPath; //这个参数表示文件在服务器的存储路径
if (osName.toUpperCase().contains("windows".toUpperCase())) {//windows系统下路径
downLoadPath = "D:\\upload\\2020-07-22\\a.jpg";
} else {
downLoadPath = fileDTO.getFilepath();
}
String contentType = "application/octet-stream";
try {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
BufferedInputStream bis;
BufferedOutputStream bos;
long fileLength = new File(downLoadPath).length();
response.setContentType(contentType);
response.setHeader("Content-disposition",
"attachment; filename=" + new String(realName.getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(fileLength));
bis = new BufferedInputStream(new FileInputStream(downLoadPath));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bis.close();
bos.close();
return new JsonResult();
} catch (Exception e) {
e.printStackTrace();
return new JsonResult(MsgCode._error);
}
}
@RequestMapping(value = "/uploadFiles", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public JsonResult uploadFiles(@RequestParam(value = "files") MultipartFile[] files, @RequestParam(value = "id") String id, HttpServletRequest request, HttpServletResponse response) {
Map res = new HashMap();
JsonResult jsonResult = new JsonResult();
try {
log.info("uploadFiles 上传文件操作>>>>>id:{}", id);
if (files != null) {
for (MultipartFile file : files) {
jsonResult = demoService.uploadFiles(file, id);
}
} else {
return jsonResult;
}
} catch (Exception e) {
log.error("uploadFiles>>>>异常:{}", e.toString());
}
log.info("uploadFiles>>>>返回结果:{}", res);
return jsonResult;
}
Service上传实现
if(OSname.toUpperCase().contains(“windows”.toUpperCase())){//windows系统下路径
path = “D:/upload/”+ date + “/” +fileName;
}else{
path = fileRelationPath+ date + “/” + fileName;
}
File localFile = new File(path);
if (!localFile.getParentFile().exists()) {
//如果目标文件所在的目录不存在,则创建父目录
localFile.getParentFile().mkdirs();
}
file.transferTo(localFile);