版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013310119/article/details/82387611
工具类:流转字符串工具类
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletResponse;
import oracle.sql.BLOB;
/***
* description: 对流数据的操作
*/
public class ReadIo2Str {
private static ReadIo2Str instance;
private ReadIo2Str() {
}
public static ReadIo2Str getInstance() {
if (null == instance) {
instance = new ReadIo2Str();
}
return instance;
}
public static void main(String[] args) {
String filePath = "D:\\1111\\111.docx";
String filePath2 = "D:\\1111\\222.docx";
String str = ReadIo2Str.getInstance().read2String(filePath);
//ReadIo2Str.getInstance().read2Io(str, filePath2);
System.out.println(str);
}
/**
* description: 将流转换成String
* @param filePath
* @return
* @author xyc
* @update 2015-6-4
*/
public String read2String(String filePath) {
String ioStr = "";
FileInputStream is = null;
try {
is = new FileInputStream(filePath);
byte[] in = new byte[is.available()];
is.read(in);
ioStr = HQCodec.hexEncode(in);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return ioStr;
}
/**
* description: 将文件流写到某一个目录下
* @param ioStr
* @param filePath
* @author xyc
* @update 2015-6-4
*/
public void read2Io(String ioStr, String filePath,String fileName) {
FileOutputStream fos = null;
try {
byte[] iobyte = HQCodec.hexDecode(ioStr);
File file = new File(filePath+fileName);
if (file.exists()) {
file.delete();
}
fos = new FileOutputStream(file);
fos.write(iobyte, 0, iobyte.length);
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != fos) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* description: 将Oracle数据库字段Blob转换成Input输入流然后转换成String
* @param filePath
* @return
* @author xyc
* @update 2015-6-4
*/
public String readStream2String(InputStream inputStream,byte[] bt) {
String ioStr = "";
InputStream is = inputStream;
try {
byte[] in = bt;
is.read(in);
ioStr = HQCodec.hexEncode(in);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return ioStr;
}
public byte[] blobToBytes(BLOB blob) {
InputStream is = null;
byte[] b = null;
try {
is = blob.getBinaryStream();
b = new byte[(int) blob.length()];
is.read(b);
return b;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
is = null;
} catch (IOException e) {
e.printStackTrace();
}
}
return b;
}
/**
* description: 将文件流写到某一个目录下,并提供对外下载接口
* @param ioStr
* @param filePath
* @author xyc
* @update 2015-6-4
*/
public void read2IoRS(String ioStr, String filePath,String fileName,HttpServletResponse response) {
FileOutputStream fos = null;
try {
byte[] iobyte = HQCodec.hexDecode(ioStr);
File file = new File(filePath+fileName);
if (file.exists()) {
file.delete();
}
fos = new FileOutputStream(file);
fos.write(iobyte, 0, iobyte.length);
fos.flush();
/**对外提供下载链接*/
/*Path fileXZ = Paths.get(filePath, fileName);
if (Files.exists(fileXZ)) {
response.setContentType("application/x-all");//ZIP压缩格式文件
try {
response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "GBK"));
Files.copy(fileXZ, response.getOutputStream());
} catch (IOException ex) {
ex.printStackTrace();
}
}*/
String filedl = filePath + fileName;
download(filedl,response);
/**对外提供下载链接结束*/
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != fos) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public HttpServletResponse download(String path, HttpServletResponse response) {
try {
// path是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return response;
}
}
/**
* description: hexcode转换
*/
public class HQCodec {
static final char[] HEX = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
'F' };
public static String hexEncode(byte[] buffer) {
if (buffer.length == 0) {
return "";
}
int holder = 0;
char[] chars = new char[buffer.length * 2];
for (int i = 0; i < buffer.length; i++) {
holder = (buffer[i] & 0xf0) >> 4;
chars[i * 2] = HEX[holder];
holder = buffer[i] & 0x0f;
chars[(i * 2) + 1] = HEX[holder];
}
return new String(chars);
}
public static byte[] hexDecode(String hex) {
//A null string returns an empty array
if (hex == null || hex.length() == 0) {
return new byte[0];
} else if (hex.length() < 3) {
return new byte[] { (byte) (Integer.parseInt(hex, 16) & 0xff) };
}
//Adjust accordingly for odd-length strings
int count = hex.length();
int nibble = 0;
if (count % 2 != 0) {
count++;
nibble = 1;
}
byte[] buf = new byte[count / 2];
char c = 0;
int holder = 0;
int pos = 0;
for (int i = 0; i < buf.length; i++) {
for (int z = 0; z < 2 && pos < hex.length(); z++) {
c = hex.charAt(pos++);
if (c >= 'A' && c <= 'F') {
c -= 55;
} else if (c >= '0' && c <= '9') {
c -= 48;
} else if (c >= 'a' && c <= 'f') {
c -= 87;
}
if (nibble == 0) {
holder = c << 4;
} else {
holder |= c;
buf[i] = (byte) holder;
}
nibble = 1 - nibble;
}
}
return buf;
}
}
DAO层,BLOB转流然后转字符串
Map<String, Object> map = new HashMap<String, Object>();
BLOB blob = (BLOB) rs.getBlob("fjnr");
InputStream inputStream = blob.getBinaryStream();
byte[] bt = ReadIo2Str.getInstance().blobToBytes(blob);
System.out.println("字节数组:"+ bt);
String str1 = ReadIo2Str.getInstance().readStream2String(inputStream,bt);//转成String字符串
map.put("wj1", str1);
Controller获得BLOB转流转字符串之后的str1,然后将str1转成文件流输出
String filePath2 = "/home/fjlc/";//指定附件保存的位置
fileName = RW_BH + fileName;//附件的名称
ReadIo2Str.getInstance().read2IoRS(wjcontent, filePath2,fileName,response);//将wjcontent字符串转成流输出成文件并提供下载
//ReadIo2Str.getInstance().read2Io(wjcontent, filePath2,fileName);//附件内容保存
具体根据自己实际需要进行修改。