angularjs java下载文件,AngularJS $ http-post-将二进制文件转换为Excel文件并下载

59642f42ed587e93c026583f5ede9cd4.png

萧十郎

只是注意到您由于IE8 / 9而无法使用它,但是我还是会继续提交...也许有人觉得它有用实际上,这可以通过浏览器使用完成blob。注意Promise中的responseType和代码success。$http({    url: 'your/webservice',    method: "POST",    data: json, //this is your json data string    headers: {       'Content-type': 'application/json'    },    responseType: 'arraybuffer'}).success(function (data, status, headers, config) {    var blob = new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});    var objectUrl = URL.createObjectURL(blob);    window.open(objectUrl);}).error(function (data, status, headers, config) {    //upload failed});尽管有一些问题,例如:它不支持IE 8和9:它会打开一个弹出窗口,以打开objectUrl可能被阻止的人生成奇怪的文件名确实有效!用以下方法测试了PHP中的服务器端代码。我确定您可以在Java中设置类似的标头:$file = "file.xlsx";header('Content-disposition: attachment; filename='.$file);header('Content-Length: ' . filesize($file));header('Content-Transfer-Encoding: binary');header('Cache-Control: must-revalidate');header('Pragma: public');echo json_encode(readfile($file));浏览器使以这种方式保存数据变得更加困难。一种不错的选择是使用filesaver.js。它为提供了跨浏览器的实现saveAs,并且应替换上述Promise中的某些代码success。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中实现文件上传的常用方式是使用HTTP协议的`multipart/form-data`格式,具体实现步骤如下: 1. 创建一个`HttpURLConnection`连接对象,设置请求方法为POST,并设置连接超时时间和读取超时时间。 2. 设置请求头信息,包括`Content-Type`、`User-Agent`、`Accept-Language`等,其中`Content-Type`设置为`multipart/form-data`。 3. 创建输出流,并将需要上传的文件写入到输出流中。在写入文件之前需要设置一个分隔符,用于分隔不同字段的内容。 4. 在输出流的末尾写入分隔符,表示文件上传结束。 5. 发送HTTP请求,并读取服务器返回的响应结果。 下面是一个Java实现文件上传的示例代码: ```java public static void uploadFile(String url, File file) throws IOException { String boundary = "---------------------------" + System.currentTimeMillis(); //设置分隔符 HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); //创建连接对象 conn.setRequestMethod("POST"); //设置请求方法为POST conn.setConnectTimeout(5000); //设置连接超时时间 conn.setReadTimeout(30000); //设置读取超时时间 conn.setDoOutput(true); //允许输出 conn.setDoInput(true); //允许输入 conn.setUseCaches(false); //不使用缓存 conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); //设置请求头信息 conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"); conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8"); OutputStream out = new DataOutputStream(conn.getOutputStream()); //创建输出流 FileInputStream fileInputStream = new FileInputStream(file); //创建文件输入流 byte[] buffer = new byte[1024]; int len = 0; out.write(("--" + boundary + "\r\n").getBytes()); //写入分隔符 out.write(("Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"\r\n").getBytes()); out.write(("Content-Type: application/octet-stream\r\n\r\n").getBytes()); while ((len = fileInputStream.read(buffer)) != -1) { out.write(buffer, 0, len); //写入文件数据 } out.write(("\r\n--" + boundary + "--\r\n").getBytes()); //写入分隔符 out.flush(); //清空缓存 fileInputStream.close(); //关闭文件输入流 out.close(); //关闭输出流 int responseCode = conn.getResponseCode(); //获取响应码 if (responseCode == 200) { InputStream inputStream = conn.getInputStream(); //获取响应输入流 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); //创建读取响应结果的缓冲流 String line; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); //输出响应结果 } bufferedReader.close(); //关闭缓冲流 inputStream.close(); //关闭输入流 } else { System.out.println("文件上传失败,响应码为:" + responseCode); } } ``` 其中,`url`为上传文件的URL,`file`为需要上传的文件。在实际使用时,需要根据实际情况更改请求头信息、分隔符和文件字段名等参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值