The picture is written on the page, or it is written on the browser, it is a file -- "stream --" byte conversion method

 

The picture is written on the page, or it is written on the browser, it is a file -- "stream --" byte conversion method

 

File--"stream--"byte, later developed with a tool class packaged part, as for the suffix name, the format limit is directly judged

ByteArrayInputStream extends  InputStream  

ByteArrayOutputStream extends OutputStream

FileOutputStream extends OutputStream

FileInputStream extends InputStream

 

file <-->InputStream<-->bytes[]//Forward use method, reverse use constructor

 

Stream becomes buffere toString after someone else's reader

 BufferedImage bufferedImage = ImageIO.read(input);

 

Stream--"byte change method (2 kinds)

After Stream's own reader becomes byte[], Stream has toByteArray method directly after becoming ByteArrayInputStream

 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);

// }

 

 

Original file -- "stream --" bytes:

@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;

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326354469&siteId=291194637