java上传下载文件,中文文件名不显示或乱码
来源:互联网 发布:linux反向域名解析 编辑:程序博客网 时间:2024/04/29 20:52
参考这里:https://my.oschina.net/pingpangkuangmo/blog/376332
上传文件页面:
<body><form method="post" action="${pageContext.request.contextPath }/fileUpload" enctype="multipart/form-data"> 选择一个文件: <input type="file" name="uploadFile" /> <br/><br/> <input type="submit" value="上传" /></form></body>
上传文件后,由springmvc处理请求:
/** * 上传文件 * @param file * @throws IOException */@RequestMapping("/fileUpload")public ModelAndView fileUpload(@RequestParam("uploadFile") CommonsMultipartFile file) {//获取文件名称String fileName = file.getOriginalFilename();//保存文件File tempfile = new File("E:/zzhtml/"+fileName);OutputStream os = null;InputStream is = null;try {is = file.getInputStream();byte[] bs = new byte[1024]; int len; os = new FileOutputStream(tempfile); while ((len = is.read(bs)) != -1) { os.write(bs, 0, len); } } catch (IOException e) {e.printStackTrace();} finally{ try { if(os!=null){ os.close(); } if(is!=null){ is.close(); }} catch (IOException e) {e.printStackTrace();} }ModelAndView mv = new ModelAndView("uploadSuccess");mv.addObject("fileName", fileName); return mv; }
下载文件页面:
<body>上传成功<br><a href="${pageContext.request.contextPath }/fileDown?fileName=${fileName}">${fileName }</a></body>
点击下载文件,后台处理:
@RequestMapping("/fileDown")public String fileDown(@RequestParam("fileName")String fileName,HttpServletResponse response) throws UnsupportedEncodingException{String path="E:/zzhtml/";String tempFileName = new String(fileName.getBytes("ISO8859-1"), "UTF-8");//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型 response.setContentType("application/x-msdownload"); //2.设置文件头:最后一个参数是设置下载文件名(假如我们叫a.pdf) response.setHeader("Content-Disposition", "attachment;fileName="+new String(tempFileName.getBytes("GBK"),"ISO8859-1")); //这里的fileName要是ISO8859-1编码 ServletOutputStream out = null; FileInputStream is = null ; //通过文件路径获得File对象 File file = new File(path +tempFileName); try { is= new FileInputStream(file); //3.通过response获取ServletOutputStream对象(out) out = response.getOutputStream(); int b = 0; byte[] buffer = new byte[1024]; while ((b=is.read(buffer,0,buffer.length))!= -1){ //4.写到输出流(out)中 out.write(buffer,0,b); } out.flush(); } catch (IOException e) { e.printStackTrace(); }finally{ try { if(is!=null){ is.close(); } if(out!=null){ out.close(); }} catch (IOException e) {e.printStackTrace();} }return null;}
0 0
- java上传下载文件,中文文件名不显示或乱码
- JSP或servlet中(以及上传下载文件)中文乱码或不显示的解决方案
- jsp页面乱码或中文文件名不显示
- java文件下载不显示中文文件名
- 【Excel】java 导出Excel 中文文件名乱码不显示问题
- java实现ftp上传下载,中文不乱码
- 文件下载中文文件名不显示
- 文件下载中文文件名不显示
- HTML中文不显示或乱码
- Java下载文件,文件名中文乱码问题
- Java 下载文件(如Excel)文件名中文乱码或缺失解决方法
- Java中FTP文件上传下载中文文件乱码问题
- TOMCAT 中文文件上传下载乱码
- WINDOWS上传中文文件名文件到LINUX显示乱码
- FileUpload实现文件上传下载3-修改文件名显示包含中文的文件
- Java文件上传下载删除(下载中文乱码问题)
- 文件下载中文文件名乱码
- 解决java 文件下载文件名没有或乱码的问题
- 拒绝采样(reject sampling)原理详解
- React Native 组件 --TextInput处理文本输入
- LINQ to SQL活学活用(3):嗅出“臭味”烟消云散
- dynamic web project与static web project区别
- mysql备份的三种方式详解
- java上传下载文件,中文文件名不显示或乱码
- OraOLEDB.Oracle.1 未注册
- Jetson TX1板载相机调用测试
- 创建带Tomcat服务的CentOS Docker镜像
- POJ2386 Lake Counting(dfs)
- LINQ to SQL活学活用(4):监视你的一举一动
- imageview的scaleType详解(附图)
- OpenGL ES 2.0 - 几种剪裁与测试
- 统一编址&独立编址&哈佛结构&冯诺依曼结构