Upload images using spring mvc

Source: Internet
Author: User

Method 1. save the image to the specified path on the hard disk and write the image content to the output stream. The public static void showPic (String pic, HttpServletResponse response) {try {InputStream inputStream = null; servletOutputStream outputStream = response. getOutputStream (); response. reset (); response. setContentType ("image/jpeg"); String savePath = ""; // system file path String globalPath = FileUploadContext. getGlobalPath (); if (globalPath = null) {globalPath = "D: // software/ /";} SavePath = globalPath + pic; try {inputStream = new FileInputStream (savePath); int bufferSize = 4096; byte [] B = new byte [bufferSize]; int n = 0; do {n = inputStream. read (B); if (n> 0) {outputStream. write (B, 0, n) ;}} while (n> 0); outputStream. flush ();} finally {if (inputStream! = Null) {inputStream. close ();} if (outputStream! = Null) {outputStream. close () ;}} catch (IOException e) {logger. error (e. getMessage () ;}} public static String uploadPic (HttpServletRequest req, String errorPic) {MultipartFile file = (DefaultMultipartHttpServletRequest) req ). getFile ("pic"); String fileName = errorPic; if (file! = Null & file. getSize ()> 0) {try {if (file. getSize ()> 10000000) {throw new BaseException ("Upload Failed: the file size cannot exceed 10 MB");} String picPath = FileUploadContext. getGlobalPath (); fileName = FileUploadContext. nextFileName (); int point = fileName. lastIndexOf (". "); fileName = fileName. substring (0, point); String realFileName = file. getOriginalFilename (); int len = realFileName. length (); point = realFileName. lastIndexO F ("."); String prefix = realFileName. substring (point, len); if (prefix. equals ("") {throw new IllegalArgumentException ("file has no extension! ");} Else if (prefix. equalsIgnoreCase (". jpg ") | prefix. equalsIgnoreCase (". gif ") | prefix. equalsIgnoreCase (". bmp ") {// system. out. println ("upload file type is" + prefix); fileName = fileName + prefix;} else {throw new IllegalArgumentException ("file type error! ");} String fileFullName = picPath + fileName; File destFile = new File (fileFullName); file. transferTo (destFile);} catch (Exception e) {logger. error (e. getMessage (); return fileName;} Method 2: Save the image to the blob field of the database (if the table data volume is large, it is not recommended) public static byte [] savePic (ServletContext context, String fileName) {byte [] buf = null; // 10k cache try {String picPath = context. getRealPath ("WEB-INF"); int point = PicPath. lastIndexOf (File. separator); picPath = picPath. substring (0, point); picPath = picPath + fileName; FileInputStream imgis = new FileInputStream (picPath); int n = 0; ByteArrayOutputStream out = new ByteArrayOutputStream (4096 ); byte [] B = new byte [4096]; try {while (n = imgis. read (B ))! =-1) {out. write (B, 0, n) ;}} finally {if (imgis! = Null) {imgis. close () ;}} buf = out. toByteArray ();} catch (IOException e) {logger. error (e. getMessage ();} return buf;} public static void showPic (HttpServletResponse response, byte [] B) {try {ServletOutputStream outputStream = response. getOutputStream (); response. reset (); response. setContentType ("image/jpeg"); try {outputStream. write (B, 0, B. length); outputStream. flush ();} finally {if (outputSt Ream! = Null) {outputStream. close () ;}} catch (IOException e) {logger. error (e. getMessage ());}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.