项目需要,不使用IO流,只能通过服务器端传字符串发送给客户端,进行文件的下载操作。同时,文件的上传工作也通过传递字符串的方式进行的。
感谢小白和冲~
以下是具体代码。
/** * 根据文件获得文件的bytes的字符串格式 * * @param filePath * @return */ public static String getFileBytes(String filePath) throws AppException { StringBuilder sb = null; byte[] btArray = null; FileInputStream fis = null; ByteArrayOutputStream bos = null; try { File file = new File(filePath); fis = new FileInputStream(file); bos = new ByteArrayOutputStream(1000); byte[] b = new byte[1000]; int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } btArray = bos.toByteArray(); sb = new StringBuilder(); for (int i = 0; i < btArray.length; i++) { byte bt = btArray[i]; sb.append(bt); sb.append(','); } sb = sb.replace(sb.length() - 1, sb.length() - 1, ""); } catch (FileNotFoundException e) { throw new AppException(CommonEnum.IOERROR.getKey(), CommonEnum.IOERROR.getValue()); } catch (IOException e) { throw new AppException(CommonEnum.IOERROR.getKey(), CommonEnum.IOERROR.getValue()); } finally { try { if (fis != null) { fis.close(); } if (bos != null) { bos.close(); } } catch (IOException e) { // TODO log } } return sb.toString(); } /** * 根据刚刚生成的byte数组转换的String,生成文件 */ public static void creatFile(String fileContext, String fileName) throws AppException { BufferedOutputStream bos = null; FileOutputStream fos = null; File file = null; try { file = new File(fileName); File paraFile = file.getParentFile(); if (!paraFile.exists()) { paraFile.mkdirs(); } fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); String[] split = fileContext.toString().split(","); byte[] tt = new byte[split.length]; for (int i = 0; i < split.length; i++) { tt[i] = new Byte(split[i]); } bos.write(tt); } catch (Exception e) { throw new AppException(CommonEnum.IOERROR.getKey(), CommonEnum.IOERROR.getValue()); } finally { try { if (bos != null) { bos.close(); } if (fos != null) { fos.close(); } } catch (IOException e) { // TODO LOG } } }
相关推荐
//会把目录作为一个file读出一次,所以只建立目录就可以,之下的文件还会被迭代到。 if (entry.isDirectory()) { new File(filePath + entry.getName()).mkdirs(); continue; } ...
要从文件" file.dat"文件中读出第10个字节到变量C中,下列哪个方法适合? A A FileInputStream in=new FileInputStream("file.dat"); in.skip(9); int c=in.read();B FileInputStream in=new FileInputStream("file...
Java 软件工程师面试资料大整合 1 Java 面霸 1 1. int 和 Integer 有什么区别? 8 2. String 和StringBuffer的区别 8 ... java实现从文件中一次读出一个字符的操作; 124 84.17. 列出一些控制流程的方法;...
} } A 第一行会产生编译错误 B 第六行会产生编译错误 C 第六行会产生运行错误 D 程序会运行和启动 A6、要从文件" file.dat"文件中读出第10个字节到变量C中,下列哪个方法适合? ( ) A FileInputStream in=new ...
zip压缩DLL 可以使用程序自己来控制 压缩,解压. c#使用代码如下: /// /// 压缩和解压文件 ... //如果文件的压缩后大小为0那么说明这个文件是空的,因此不需要进行读出写入 if (theEntry.CompressedSize == 0) ...
'WriteFile 把哪个文件写进去,写到哪个文件里面去 Sub WriteFile(AppName As String, AppExeName As String) Dim i As Integer, i2 As Integer, BB() As Byte, CC() As Byte '读出文件1到BB i = FreeFile
这里不需要安装Prodave软件,只要把“w95_s7.dll”文件拷贝到您程序相应的目录中去即可。2.通讯设置:打开“控制面板”中“设置PG/PC接口”选项,在“应用程序访问点”列表框内选择“S7ONLINE (STEP7) PC/PPI Cable...
例如当菜单转中之后会有WM_COMMAND消息发送,WPARAM的高字中(HIWORD(wParam))是命令的ID号,对菜单来讲就是菜单ID。当然用户也可以定义自己的消息名称,也可以利用自定义消息来发送通知和传送数据。 2、谁将收到...
从linux 内核0.95 版后已经使用与现在相同的命名方法了。 entry start ! 告知连接程序,程序从start 标号开始执行。 start: ! 47--56 行作用是将自身(bootsect)从目前段位置0x07c0(31k) ! 移动到0x9000(576k)处,...
2、Ds1302_Read_Byte -----从DS1302读出一字节数据 3、Ds1302_Write_Time ------向DS1302写入时钟数据 4、Ds1302_Read_Time -------从DS1302读出时钟数据 5、Ds1302_Init ------------------- DS1302初始化 十一、...
* Function String ReadCardInfo( long CommID , long CardType , long MF1_Area ) Library "MF1_AnLock2009.dll"; ************************************************************** 例1: 假设端口号为3,当卡片...
1,连接数据库代码 文件名称 conn.asp 所有访问数据库的文件都调用此文件<!--#include file=\"Conn.asp\"--> db=\"data/data.mdb\" \'数据库存放目录 on error resume next set conn=server.createobject(\...