大佬们,我想要做的是下载限速。公司将软件上传,对其下载网速进行限速。现在限速下载接口算是写好了,前端访问后台页面并将文件下载下来我不会写。问的公司有经验的一个后端,现在现在效果是页面上显示访问成功,后台代码显示下载成功。但是找不到下载的文件在哪。请大佬们帮我看看前端代码怎么改。我第一次写代码,前端不会,后端不熟,呜呜呜,大佬们帮帮忙把。
下边是下载的代码
@Controller
@RequestMapping("file")
public class DownloadFileController {
@PostMapping("/limit")
public void limitDownloadFile(String file, HttpServletResponse response) throws IOException {
System.out.println("download file");
if (file == null) {
file = "C:/Users/jhsoft/Desktop/test.txt";
}
File downloadFile = new File(file);
System.out.println(downloadFile.getName());
FileInputStream fileInputStream = new FileInputStream(downloadFile);
response.setContentType("application/x-msdownload;");
response.setHeader("Content-disposition", "attachment; filename=" + new String(downloadFile.getName()
.getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(downloadFile.length()));
ServletOutputStream outputStream = null;
try {
LimitInputStream limitInputStream = new LimitInputStream(fileInputStream, new BandwidthLimiter(1024));
long beginTime = System.currentTimeMillis();
outputStream = response.getOutputStream();
System.out.println("outputStream:");
System.out.println(outputStream.toString());
byte[] bytes = new byte[1024];
int read = limitInputStream.read(bytes, 0, 1024);
while (read != -1) {
outputStream.write(bytes);
read = limitInputStream.read(bytes, 0, 1024);
}
System.out.println(System.currentTimeMillis() - beginTime);
} finally {
fileInputStream.close();
if (outputStream != null) {
outputStream.close();
}
System.out.println("download success!");
}
}
}
这是前端内容:
<script type="text/javascript">
//页面一开始加载的时候会先执行这个方法里的东西
$(function (){
$.ajax({
type: "post",
url: "/file/limit",
data: {
"file": "F:/项目restaurant/restaurant/restaurant/sql/text.txt"
},
success: function(res) {
console.log("访问成功");
}
});
});
这种事情干嘛不直接搞个路由器?
技术选型谁做的.
哪有人真靠代码限速的?
搞一个下载站,做一下鉴权.然后在下载站外面套一个软路由,在软路由上做限速
下载接口返回授权过的下载链接给前端,
前端直接访问下载站,.
什么鬼?正常如果你的action返回的是file,那么实际上,你window.open开这个action,浏览器就该正常让你保存才对。。。
可能保存了 找不到保存的文件在哪里