话不多说,直接上代码
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
public class LoadFile {
public ResponseEntity loadFile() {
String key = "要下载的内容";
Resource resource = new ByteArrayResource(key.getBytes());
/**
* 注:此处只要有resource对象就可以了,不必按我上面获取二进制搞法,上面仅是样例
*/
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType("application/octet-stream"))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename= 下载的文件名.txt")
.body(resource);
}
}