Java FTP下载文件以及编码问题小结

之前在开发过程中,遇到了一点问题,我要访问一个FTP服务器去下载文件详细情况如下:

1. 需要传入一个可能为中文的文件名;

2. 通过文件名去FTP上寻找该文件;

3. FTP服务器的命名编码为“GBK”;

思路

1.通过GET方法直接将文件名负载URL后面,但需要通过转码;

2.在Java Controller中收到参数后,进行解码,解码为正常数据;

3.用正常数据再转码为GBK,到Service中去调用FTP即可

4.(因公司安全考虑,我们需要在另一个模块中调用FTP)通过rest接口将文件名传出,另一模块获取到文件流转换为byte[]传回,调用response输出即可

总结 编码问题的解决方案:

Jquery对URL以及参数转码,据我所了解的主要应用encodeURI、encodeURIComponent,例如我需要传入变量名为fileDepence

var downloadDepence=fileID+"-"+filename; window.location.href=encodeURI(ajaxurl+"/coadownload/downloadFile?fileDepence="+encodeURIComponent(downloadDepence));

这样我在后台就可以接收到转码过后的fileDepence这个串,通过验证encodeURIComponent会以“utf-8”进行转码,所以我们使用Java对其解码:

String viewItem=java.net.URLDecoder.decode(fileDepence, "utf-8");

这样得到的viewItem就与我们原本要传入的值一致了,如果传入的为中文文件名,则此时viewItem便是对应的中文文件名了。

之后我又了解一下,通过JS来完成GBK的转码比较麻烦,而采用Unicode的Java则比较方法,则同理,我们使用viewItem在以GBK来转一次码,就可以得到对应的FTP服务器中的文件名了。

String GBKItem= URLEncoder.encode(viewItem,"GBK");

FTP的解决方法

建立一个FTP连接的公用类

public static FtpClient connectFTP(String url, int port, String username, String password) { //创建ftp FtpClient ftp = null; try { //创建地址 SocketAddress addr = new InetSocketAddress(url, port); //连接 ftp = FtpClient.create(); ftp.connect(addr); //登陆 ftp.login(username, password.toCharArray()); ftp.setBinaryType(); } catch (FtpProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } logger.info("已登录到FTP服务器!" + new Date()); return ftp; }

在另一个模块的Service中建立一个下载的方法,在rest接口访问的Controller中调用该方法。

public FileBtye downloadfilefromFTP(String fileDepence,HttpServletResponse response) throws IOException, FtpProtocolException { FileBtye file=new FileBtye(); //FileBtye为一个只包含btye[]的对象,主要用于适合Controller的responsebody的json传出 response.setCharacterEncoding("utf-8"); fileDepence=fileDepence.replace("\"", ""); //rest接口传输过来的值需要做一定的格式化处理 FtpClient ftpClient= FTPManager.connectFTP(FTPHost, Integer.parseInt(FTPPort), FTPUserName, FTPPassWord); //通过properties文件读取 InputStream is = null; ftpClient.changeDirectory("//filepath"); //文件目录 is = ftpClient.getFileStream(fileDepence); //获取文件流 ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); byte[] buff = new byte[100]; //buff用于存放循环读取的临时数据 int rc = 0; while ((rc = is.read(buff, 0, 100)) > 0) { swapStream.write(buff, 0, rc); } byte[] fileByte = swapStream.toByteArray(); file.setFileinfo(fileByte); is.close(); return file; }

在本地Service中调用rest接口完成输出response输出即可,同时要注意response的设置

       String viewItem=java.net.URLDecoder.decode(fileDepence, "utf-8"); //FTP服务器命名规则以GBK编码 String GBKItem= URLEncoder.encode(viewItem,"GBK");
       //调用rest接口,拿到返回的FileBtye FileBtye downloadStream
=restTemplate.postForObject(gatewayRootUrl + GatewayUrlConstant.COA_DOWNLOAD + GatewayUrlConstant.DOWNLOAD_FILE, GBKItem, FileBtye.class); if(downloadStream!=null){ //设置信息头 response.setCharacterEncoding("utf-8"); //设置编码 response.setContentType("multipart/form-data"); //根据文件确定文件格式 response.setHeader("Content-Disposition", "attachment;fileName=" + viewItem); //设置文件名 ServletOutputStream out; //输出流输出 out = response.getOutputStream(); out.write(downloadStream.getFileinfo()); out.flush(); out.close(); logger.info("下载文件成功" + new Date()); }else { logger.info("下载文件不存在" + new Date()); }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/badca33e814963c42320eb43294ed830.html