我必须尝试下载我们系统中的文件。为此,我使用了弹簧靴和角度 js 1。文件已下载,但未打开,并显示消息解释 JPEG 图像文件时出错(不是 JPEG 文件:以 0xef 0xbf 开头(。我的示例代码是 -
DownloadService.java
import com.codahale.metrics.annotation.Timed;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.io.*;
import java.net.URLConnection;
@Component
@RequestMapping("/api/1/download")
@Path("/api/1/download")
@Consumes(MediaType.APPLICATION_JSON)
@Slf4j
public class DownloadResource {
@ApiOperation(value = "downloads selected file", notes = "Returns a file", responseContainer = "FileSystemResource", response = HttpServletResponse.class)
@Path("/downloadFile")
@GET
@Timed
public void downloadFile(@Context HttpServletResponse response) throws IOException {
String filePath = ("/home/ashish/Desktop/CTA.jpg");
File fileName = new File(filePath);
String mimeType = URLConnection.guessContentTypeFromName(fileName.getName());
if (mimeType == null) {
System.out.println("mimetype is not detectable, will take default");
mimeType = "application/octet-stream";
}
response.setContentType(mimeType);
response.setHeader("Content-Disposition", String.format("inline; filename="" + fileName.getName() + """));
response.setContentLength((int) fileName.length());
InputStream inputStream = new BufferedInputStream(new FileInputStream(fileName));
FileCopyUtils.copy(inputStream, response.getOutputStream());
inputStream.close();
}
}
download.js
app.controller('storeReportsCtrl', function($rootScope, $scope, Restangular, $state, $stateParams, $location, $modal, $log, $timeout,$http,$sce) {
$scope.download = function(fileName) {
$http.get('/api/1/download/downloadFile', {responseType:'arraybuffer'})
.success(function (response) {
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
var fileName = "ppt2.jpg";
// var mimeType = data.mimeType;
var blob = new Blob([response], {type: 'image/jpeg'}),
url = window.URL.createObjectURL(blob);
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
}).catch(function(error) {
console.log(error);
});
}
});
从此文件下载,但它已损坏,这意味着我的原始文件大小为 18,394 字节,响应.byteLength = 33496。
更新
相同的代码对于文本文件工作正常,但对于二进制文件则不起作用。
如何下载正确的文件..
String filePath = ("/home/ashish/Desktop/CTA.jpg"(;文件路径是我认为的问题,并确保该文件存在
@GetMapping("/downloadFile")
public void doima(HttpServletResponse response) throws IOException {
String filePath = ("\home\ashish\Desktop\CTA.jpg");
File fileName = new File(filePath);
String mimeType = URLConnection.guessContentTypeFromName(fileName.getName());
System.out.println(mimeType);
if (mimeType == null) {
System.out.println("mimetype is not detectable, will take default");
mimeType = "application/octet-stream";
}
response.setContentType(mimeType);
response.setHeader("Content-Disposition", String.format("inline; filename="" + fileName.getName() + """));
response.setContentLength((int) fileName.length());
InputStream inputStream;
try {
inputStream = new BufferedInputStream(new FileInputStream(fileName));
FileCopyUtils.copy(inputStream, response.getOutputStream());
inputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
CTA.jpg存在于您的资源路径中,那么您可以尝试像下面一样
InputStream in = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("CTA.png");