@Controller
public class FileDownLoadController {
@RequestMapping(value="download.htm",method = RequestMethod.POST)
String filepathString = request.getParameter("filePath");
response.setContentType("multipart/form-data");
//设置下载文件的表头并对其中的中文字符进行处理(如不处理,中文名是无法显示的)
response.setHeader("Content-Disposition", "attachment;filename=" +new String( filenameString.getBytes("GBK"), "ISO8859-1"));
try {
File file=new File(filenameString);
//打印绝对路径
//System.out.println(file.getCanonicalPath());
InputStream inputStream=new FileInputStream(filepathString+"\\"+file);
OutputStream os=response.getOutputStream();
byte[] b=new byte[1024];
int length;
while((length=inputStream.read(b))>0){
os.write(b,0,length);
}
inputStream.close();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
public class FileDownLoadController {
@RequestMapping(value="download.htm",method = RequestMethod.POST)
public @ResponseBody String FileDownLoad(HttpServletResponse response,HttpServletRequest request) throws UnsupportedEncodingException{
//这里是从前台页面获取到的文件名和文件路径(我是把这两个属性上传的时候存到了数据库里)
String filenameString = request.getParameter("fileName");String filepathString = request.getParameter("filePath");
response.setContentType("multipart/form-data");
//设置下载文件的表头并对其中的中文字符进行处理(如不处理,中文名是无法显示的)
response.setHeader("Content-Disposition", "attachment;filename=" +new String( filenameString.getBytes("GBK"), "ISO8859-1"));
try {
File file=new File(filenameString);
//打印绝对路径
//System.out.println(file.getCanonicalPath());
InputStream inputStream=new FileInputStream(filepathString+"\\"+file);
OutputStream os=response.getOutputStream();
byte[] b=new byte[1024];
int length;
while((length=inputStream.read(b))>0){
os.write(b,0,length);
}
inputStream.close();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}