图片写到页面上,还是写到浏览器上都是文件--》流--》字节转化方法
2017-04-17 16:03
344 查看
图片写到页面上,还是写到浏览器上都是文件--》流--》字节转化方法
文件--》流--》字节,后面发展用工具类包装了部分,至于后缀名,格式的限制就直接判断
ByteArrayInputStream extends InputStream
ByteArrayOutputStream extends OutputStream
FileOutputStream extends OutputStream
FileInputStream extends InputStream
file <-->InputStream<-->bytes[]//正向用方法,逆向用构造函数
Stream 放在别人的reader之后变成buffere toString
BufferedImage bufferedImage = ImageIO.read(input);
流--》字节变化方法(2种)
Stream 自身的reader之后变成byte[],Stream变成ByteArrayInputStream后直接有toByteArray方法
destFile = new File(root,fileFileName);
// os = new FileOutputStream(destFile);
// byte[] buffer = new byte[10240];
// int length=0;
// while((length=is.read(buffer))>0){
// os.write(buffer, 0, length);
// }
原始 文件--》流--》字节:
@RequestMapping(value = "/account/tbCusFirmChg/uploadSave")
public String uploadSave(@RequestParam Map<String,Object> upMap,@RequestParam(value="file") MultipartFile file,Model model, HttpSession session, HttpServletRequest request,HttpServletResponse response) throws EsteelException {
String filePath=StaticVariables.Base_Path;
String url="";
File destFile=null;
InputStream is=null;
OutputStream os=null;
OutputStream os1=null;
String fileFileName=file.getOriginalFilename();
is= file.getInputStream();
String path=new SimpleDateFormat("yyyyMMdd").format(new Date());
String root =filePath+"/"+path;
File f = new File(root);
if(!f.exists()){
f.mkdirs();
}
String extendName="";
if(!"".equals(fileFileName)){
extendName= file.getOriginalFilename().substring(file.getOriginalFilename().indexOf(".")+1);
extendName=extendName.toUpperCase();
}
if(",BMP,JPG,GIF,".indexOf(","+extendName+",")>0){
lo = file.getSize();
if(lo>StaticVariables.maxSize_PicFile){
String sc="<script language=javascript>alert('上传的文件夹大小为"+file.getSize()/1024L+"K,超过大小限制 "+StaticVariables.maxSize_PicFile/1024L+"K! ');history.back(); </script>";
ResponseUtil.print(sc, response);
return null;
}
}
String newFileName = new SimpleDateFormat("HHmmss").format(new Date())+getRandStr("",6);
fileFileName=newFileName+"."+extendName;
destFile = new File(root,fileFileName);
os = new FileOutputStream(destFile);
byte[] buffer = new byte[10240];
int length=0;
while((length=is.read(buffer))>0){
os.write(buffer, 0, length);
}
}
@RequestMapping(value = "/account/tbCusFirmChg/showImage")
public void showReportImage(@RequestParam(value = "path") String path,
HttpServletRequest request,HttpServletResponse response) throws IOException, NumberFormatException, EsteelException {
// response.setContentType("image/jpeg");
response.setCharacterEncoding("UTF-8");
// String filePath= WebConfig.get("filePath");
String pathBase = StaticVariables.Base_Path;
File file=null;
if(path!=null&&!"".equals(path)){
file=new File(pathBase+path);
}
if( file==null||!file.exists()){
response.getWriter().print("未找到图片");
}else {
response.setContentType("image/jpeg");
FileInputStream fos = new FileInputStream(file);
byte[] bytes = new byte[1024*1024];
int length = 0;
while((length=fos.read(bytes))!=-1){
response.getOutputStream().write(bytes,0,length);
}
}
}
后面发展用工具类包装了部分:
/*存储文件返回格式tfsFileName*/
public static String uploadMethod6(MultipartFile picture)
throws IOException {
String tfsFileName = "";
String fileName="";
if (!picture.isEmpty()) {
/*String path = request.getSession().getServletContext().getRealPath("/") + File.separator + "upload"
+ File.separator + indentity;*/
fileName = picture.getOriginalFilename();
tfsFileName = TFSUtil.saveTfsByteFile(picture.getBytes(), fileName);
/*pathnew = File.separator + "upload" + File.separator + indentity + File.separator + fileName;
System.out.println(fileName);
File targetFile = new File(path, fileName);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
picture.transferTo(targetFile);*/
}
return tfsFileName;
}
/*存储文件后返回在TFS中存储的文件码*/
public static String saveTfsByteFile(byte[] fileBytes, String fileName) {
TfsManager tfsManager = getTfSManager();
String fileExt = getFileExt(fileName);
String tfsfileName = tfsManager.saveFile(fileBytes, null, fileExt, true);
return tfsfileName;
}
新建文件夹__10_.zip (9.2 KB)
下载次数: 0
相关文章推荐
- 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
- 图片缩放。。以及将文件(file)转化成byte[]的方法
- asp.net下CKFinder IE9以下浏览器中上传图片文件时提示“无效文件名或文件夹名称”的解决方法
- 如果不使用 Navigate2 的方法去访问现有页面,如何将JS写到该浏览器中???
- 对于当浏览器通过ajax加载的文件还是修改前的文件的处理方法
- 将FORM转化成一个图片,并转存到系统中的某个页面下面,并实现打印方法
- [置顶] Android开发之将图片文件转化为字节数组字符串,并对其进行Base64编码处理
- 使用Form验证,未登录时登录页面无法显示图片、css、js文件的解决方法
- 在进行页面的DIV CSS排版时,遇到IE6(当然有时Firefox下也会偶遇)浏览器中的图片元素img下出现多余空白的问题绝对是常见的对于该问题的解决方法也是“见机行事”。
- 分配粒度和内存页面大小(x86处理器平台的分配粒度是64K,内存页是4K,所以section都是0x1000对齐,硬盘扇区大小是512字节,所以PE文件默认文件对齐是0x200)
- windows下Python实现将pdf文件转化为png格式图片的方法
- css文件更新后浏览器网页仍然显示以前的页面解决方法
- 将图片转化为字节数组更好的方法
- 生成黑白画笔,android使用系统浏览器打开网页,将Bitmap图片存储在本地JPG文件的方法
- 从数据库提取图片文件显示到前端页面的一种方法
- SQL Server:将6字节的十六进制页面号转化成【文件号:页面号】格式函数
- 用C#编程从数据库中读取图片数据导进Excel文件的方法
- 一个简单的在页面处理图片大小的方法
- 晕!我还是没办法改页面顶部的背景图片
- Html+Asp+Php+Jsp:4种语言禁止浏览器缓存页面的方法