请教一下我这段文件下载的代码是否有问题
@GetMapping(value = "/test1")
// @ResponseBody
public void test1(HttpServletResponse response) throws XmlPullParserException, NoSuchAlgorithmException, InvalidKeyException, IOException {
try {
// //本地
// MinioClient minioClient = new MinioClient("http://localhost:9000", "Z3TFF3HKCMFZP5BRN62T",
// "GOIuA5leqkPEZDTi2+t2dpM3kc7XiYUoNWb+0E9r");
//服务器
MinioClient minioClient = new MinioClient("http://localhost:9000", "Z3TFF3HKCMFZP5BRN62T",
"GOIuA5leqkPEZDTi2+t2dpM3kc7XiYUoNWb+0E9r");
// Check whether the object exists using statObject().
// If the object is not found, statObject() throws an exception,
// else it means that the object exists.
// Execution is successful.
minioClient.statObject("asiatrip", "asiaphotos.zip");
// Get input stream to have content of 'my-objectname' from 'my-bucketname'
// 配置文件下载
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
// 下载文件能正常显示中文
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("asiaphotos.zip", "UTF-8"));
OutputStream outputStream = response.getOutputStream();
InputStream stream = minioClient.getObject("asiatrip", "asiaphotos.zip");
// Read the input stream and print to the console till EOF.
byte[] buf = new byte[16384];
int bytesRead;
while ((bytesRead = stream.read(buf, 0, buf.length)) >= 0) {
System.out.println(new String(buf, 0, bytesRead));
outputStream.write(buf, 0, bytesRead);
}
response.flushBuffer();
// Close the input stream.
stream.close();
outputStream.close();
} catch (MinioException e) {
System.out.println("Error occurred: " + e);
}
}相关推荐
Java程序部署到服务器上,接口请求下载文件失败/文件为空/文件名不对的问题
主要介绍了Java程序部署到服务器上,接口请求下载文件失败/文件为空/文件名不对,本文给大家分享错误原因及解决方法,需要的朋友可以参考下
asp.net 文件下载实现代码
件下载代码,Copy 别人的.需要的朋友可以参考下。
php下载文件的代码示例
复制代码 代码如下: <?php $file = ‘monkey.gif’; if (file_exists($file)) { header(‘Content-Description: File Transfer’); header(‘Content-Type: application/octet-stream’); header(‘Content-Disposition: attachment; filename=’.basename($file)); header(‘Content-Transfer-Encoding: binary’); header(‘Expires: 0’); he
下载代码
public void download3(String a,String d){ String s1 = "http://58.177.123.82:9080"+a; InputStream is = null; //定义一个输入流。 BufferedInputStream bis = null;//定义一个带缓冲的输入流 。 try{ URL url = new URL(s1);//创建一个URL对象。 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); System.out.println(); conn.setDoInput(true); conn.connect(); is = conn.getInputStream();//打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream。 bis = new BufferedInputStream(is); }catch(Exception e){ System.out.println(e.toString()); } //写到本地 BufferedOutputStream bos = null; //定义一个带缓冲的输出流。 File file = new File("/sdcard/"+d); System.out.println(file.exists()); if (file.exists()){ Toast.makeText(Reader5.this, "sorry! the file is exists", Toast.LENGTH_LONG).show(); } else{ try{ bos = new BufferedOutputStream(new FileOutputStream(file));; int num = -1; byte[] buffer = new byte[1024]; while (true) { num = bis.read(buffer); if (num == -1) { bos.flush(); break; } bos.write(buffer, 0, num); bos.flush(); } bos.close(); bis.close(); bos = null; bis = null; if( file.exists()){ Toast.makeText(Reader5.this, "download success!", Toast.LENGTH_LONG).show(); } }catch(Exception e){ } } }
PHP——文件下载
这个代码是用来用户php文件下载的,挺不错,大家可以用用,很好用的啊


Web 开发

