简单的上传文件和下载文件

//简单的上传文件和下载文件:

request.setCharacterEncoding("utf-8"); / *设置编码格式 */
response.setContentType("application/msword");  /*使浏览器能区分数据的种类,这里是word文件类型*/
Part part = request.getPart("files"); /*取的需要上传的文件*/


String path = this.getServletContext().getRealPath("/download/副本.jpg"); /*取的要下载文件的地址*/
String filename = path.substring(path.lastIndexOf("\")+1); /*取的文件名*/
response.setHeader("content-disposition","attachment;filename=" + URLEncoder.encode(filename,"utf-8")); 
int len = 0;
byte[] bfft = new byte[1024];
InputStream input = new FileInputStream(path);
 
OutputStream output = response.getOutputStream();
 
while((len = input.read(bfft))>0) {
output.write(bfft, 0, len);
}
原文地址:https://www.cnblogs.com/sjyzz/p/6536688.html