您的位置:首页 > 其它

图片写到页面上,还是写到浏览器上都是文件--》流--》字节转化方法

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐