SSM/angularjs _ 文件的上传下载

来源:互联网 发布:完美芦荟胶淘宝官网 编辑:程序博客网 时间:2024/05/01 15:11

后台

package com.bayside.npa.resource.controller;


import java.io.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.bayside.npa.resource.model.Resource;
import com.bayside.npa.resource.service.impl.ResourceImpl;
import com.bayside.npa.util.UuidUtil;

@RestController
public class FileController {
/**
* 文件上传功能
*
* @param file
* @return
* @throws IOException
*/
@Autowired
private ResourceImpl rImpl;
@RequestMapping(value = "/uploadFile", method = { RequestMethod.GET,RequestMethod.POST }, produces = "text/html;charset=UTF-8")
public String uploadFile(MultipartHttpServletRequest multipartRequest, HttpServletRequest request, Resource res,Integer firmsid) {
InputStream in = null;
String newfileName = null;
String newpathname = null;
String resourcename = null;
String filePath2 = null;
String fileAddre = "/numUp";
//MultipartHttpServletRequest multipartRequest。。。。multipartRequest.getFile("inputfile"); 用来接收上传过来的文件
MultipartFile file = multipartRequest.getFile("inputfile");
if (file.isEmpty()) {
try {
System.out.println("文件不存在!");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
try {
in = file.getInputStream();
} catch (IOException e) {
System.out.println(e.getMessage());
}
try {
InputStream stream = file.getInputStream();// 把文件读入
String filePath = request.getServletContext().getRealPath(fileAddre);// 取系统当前路径
// 添加了自动创建目录的功能
File file1 = new File(filePath);
((File) file1).mkdir();
//新命名文件
newfileName = System.currentTimeMillis()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf('.'));
filePath2 = "numUp"+"\\"+newfileName;//存储相对路径到数据库
newpathname = filePath + "\\" + newfileName;//存储文件的路径
OutputStream bos = new FileOutputStream(newpathname);
resourcename = file.getOriginalFilename();//上传时的文件名字
int bytesRead = 0;
byte[] buffer = new byte[81920];
while ((bytesRead = stream.read(buffer, 0, 81920)) != -1) {
bos.write(buffer, 0, bytesRead);// 将文件以字节流的方式写入服务器
}
bos.close();//释放流
stream.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

//封装
res.setId(UuidUtil.getUUID());
res.setResourceurl(filePath2);
res.setFirmsid(firmsid);
res.setResourcename(resourcename);

rImpl.insertSelective(res);

return "";

}

/**
* 文件下载功能
* @param request
* @param response
* @throws Exception
*/
@RequestMapping(value = "/downloadFile", method = { RequestMethod.GET,RequestMethod.POST })
public String downloadFile(String resourcename, String resourceurl,HttpServletRequest request, HttpServletResponse response) {
if (resourcename != null) {
String realPath = request.getServletContext().getRealPath(resourceurl);
File file = new File(realPath);
if (file.exists()) {
response.setContentType("application/force-download");
response.addHeader("Content-Disposition","attachment;fileName=" + resourcename);// 设置文件名
byte[] buffer = new byte[81920];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
return null;
}

}


————————————前端—————————————

<!-- html    下载-->

<tbody>
<tr ng-repeat="list in resourceList">
<td ng-bind="list.resourcename"></td>
<td>
<button type="button" class="btn btn-primary btn-xs" style="width: 80px;" ng-click="filedown(list.resourceurl,list.resourcename)">下载</button>
<button type="button" class="btn btn-primary btn-xs" style="width: 80px;">打印</button>
<button id="btn-shan" type="button" class="btn btn-primary btn-xs" style="width: 80px;" ng-click="delResource(list.id)">删除</button>
</td>
</tr>
</tbody>

<!--html   上传-->

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title" id="myModalLabel">上传文件</h4>
</div>
<div class="modal-body">
<div class="modal-body-left">
<ul>
<li>
<span class="wenjian-name">文件:</span>
<span class="wenjian-input"><input type="file" id="upfile" name="upfile" na-madel="test" /></span>
</li>
</ul>
</div>
<div class="modal-body-right">
<img id="tianjia" src="../index/img/tianjia.png" />
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
<button id="btn-queding" type="button" class="btn btn-primary" onclick="fileUpload()">确定</button>
</div>
</div>
</div>
</div>
</div>
<footer>
<div class="container">


</div>
</footer>
<script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="../js/ajaxfileupload.js"></script>
<script type="text/javascript" src="../js/bootstrap.min.js"></script>
<script type="text/javascript" src="../common/js/angular.min.1.3.2.js"></script>
<script type="text/javascript" src="../common/js/common.js"></script>
<script type="text/javascript" src="../app/js/xcConfirm.js"></script>
<script type="text/javascript" src="../app/js/getUrlParam.js"></script>
<script type="text/javascript" src="js/resourceservice.js"></script>
<script type="text/javascript" src="js/resource.js"></script>


<script>
//上传文件按钮
$("#btn-shangchuan").click(function(){
$(".modal-body-left ul").html('<li><span class="wenjian-name">文件:</span><span class="wenjian-input"><input type="file" name="inputfile" id="inputfile"></span></li>')
});
</script>


<!--js  -->


var cod = "0";
var countdown = 60;
var firmsid = UrlParm.parm("firmsid");
//上传功能
function fileUpload() {
$.ajaxFileUpload({
             url:baseUrl+"uploadFile",
             data:{"firmsid":firmsid},
             fileElementId:'inputfile',
             cache:false,
             secureuri:false, 
             contentType:"application/x-www-form-urlencoded;charset=utf-8",
             dataType:'TEXT', 
       
  })
}

var app = angular.module("resourceApp", [ "resourceServiceApp" ]);

app.controller("resourceCtrl",
function($scope, $http, resourceService, $interval) {
$scope.resource = {};
$scope.resourceList = {};
var tag = true;
$scope.resource.firmsid = firmsid;

$scope.showList = function(pageNum,pageSiez){
var data = new Object;
data.pageNum = pageNum;
data.pageSize = pageSiez;
data.firmsid = firmsid;
resourceService.showList(data).success(function(response) {
if (response.succeed) {
$scope.resourceList = response.object;
}
})
}
$scope.showList(1,10);

$scope.delResource = function(id){
var data = new Object;
data.id=id;
resourceService.delResource(data).success(
function(response){
if(response.succeed){
$scope.showList(1,10);
}
})

}

//下载功能
$scope.filedown = function(resourceurl,resourcename){
var data = new Object;
data.resourceurl = resourceurl;
data.resourcename = resourcename;
window.open("../downloadFile?resourceurl="+resourceurl+"&resourcename="+resourcename);

}

// 解决浏览器缓存
function timestamp(url) {
// var getTimestamp=Math.random();
var getTimestamp = new Date().getTime();
if (url.indexOf("?") > -1) {
url = url + "&timestamp=" + getTimestamp
} else {
url = url + "?timestamp=" + getTimestamp
}
return url;
}
})

1 0
原创粉丝点击