【Spring boot】文件上传和下载Controller代码
【摘要】
下载
import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.ServletOutputStream;import javax...
下载
-
import org.springframework.stereotype.Controller;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
-
import javax.servlet.ServletOutputStream;
-
import javax.servlet.http.HttpServletResponse;
-
import java.io.*;
-
import java.net.URLEncoder;
-
-
@Controller
-
@RequestMapping("/down")
-
public class DownLoadController {
-
@RequestMapping("/load")
-
public void download(HttpServletResponse response) throws UnsupportedEncodingException {
-
String s = URLEncoder.encode("中文.zip", "utf-8");
-
// 设置content-type:二进制
-
response.setHeader("Content-Type", "application/octet-stream");
-
// response.setContentType("application/octet-stream");
-
response.setHeader("Content-Disposition","attachment;filename="+s);
-
try {
-
FileInputStream inputStream = new FileInputStream("d:/his02-0.3.war");
-
BufferedInputStream bufferinput = new BufferedInputStream(inputStream);
-
ServletOutputStream outputStream = response.getOutputStream();
-
BufferedOutputStream bufferedOutput = new BufferedOutputStream(outputStream);
-
byte[] bytes = new byte[1024];
-
int len=bufferinput.read(bytes);
-
while (len!=-1){
-
bufferedOutput.write(bytes,0,len);
-
bufferedOutput.flush();
-
len=bufferinput.read(bytes);
-
}
-
bufferinput.close();
-
bufferedOutput.close();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
}
文章来源: blog.csdn.net,作者:一条coding,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/skylibiao/article/details/99824799
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)