How to save a picture after receiving it in Java background

Time:2020-1-17

Java is an object-oriented programming language. It not only absorbs the advantages of C + + language, but also discards the concepts of multi inheritance and pointer which are difficult to understand in C + +. Therefore, Java language has two characteristics: powerful function and easy to use. As a representative of the static object-oriented programming language, Java language implements the object-oriented theory perfectly, and allows programmers to program complex programs in an elegant way of thinking.

Java has the characteristics of simplicity, object-oriented, distributed, robust, security, platform independence and portability, multithreading, dynamic and so on. Java can write desktop applications, web applications, distributed systems and embedded system applications.

  • The first step: with the help of spring MVC framework platform.
  • Step 2: how to get the file name from the Java Web page download function.
  • Step 3: how to achieve breakthrough preview effect in the front end.

The second step is to realize the main functions. Spring boot is integrated with spring MVC by default. Using spring boot is the same as using spring MVC to upload directly. Spring boot is integrated with spring MVC by default. Using spring boot is the same as using spring MVC to upload directly.

2. Front end code:

1. The specific codes are as follows:

The forms used directly here are submitted synchronously.

<!DOCTYPE html>

<html>




<head>

< title > picture upload < / Title >

<meta name="keywords" content="keyword1,keyword2,keyword3"></meta>

<meta name="description" content="this is my page"></meta>

<meta name="content-type" content="text/html; charset=UTF-8"></meta>

</head>




<body>

<form enctype="multipart/form-data" method="post" action="/testUploadimg"> 

Picture: < input type = "file" name = "file" / > < br / > 

< input type = "submit" value = "Upload" / >

</form>

</body>




</html>

 

Controller uploadcontroller implementation

The uploadcontroller is mainly divided into three parts

1.1 adjust page request gouploadimg

1.2 upload request method uploadimg

1.3 store image method UploadFile

@Controllerpublic class UploadController {  

//Jump to the upload page  

@RequestMapping(value = "/gouploadimg", method = RequestMethod.GET)  

public String goUploadImg() {    

//Jump to uploadimg.html in the templates directory    

return "uploadimg";  

}  

//Process file upload  

@ResponseBody // JSON data returned  

@RequestMapping(value = "/testUploadimg", method = RequestMethod.POST)  

public String uploadImg(@RequestParam("file") MultipartFile file,              

HttpServletRequest request) {    

tring contentType = file.getContentType();    

String fileName = file.getOriginalFilename();    

String filePath = "D:/img";    

if (file.isEmpty()) {      

Return "the file is empty! ";    

}    

try {      

uploadFile(file.getBytes(), filePath, fileName);    

} catch (Exception e) {      

// TODO: handle exception    

}    

// return to JSON    

Return "upload succeeded";  

}  

public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {    

File targetFile = new File(filePath);    

if (!targetFile.exists()) {      

targetFile.mkdirs();    

}    

FileOutputStream out = new FileOutputStream(filePath +"/"+ fileName);    

out.write(file);    

out.flush();    

out.close();  

}

}

2: At the same time, the original file name, storage file name and associated ID of the uploaded image need to be stored in a data table.

2.1 set the storage file name to UUID to avoid duplicate storage file names


public static String getUUID(){

    UUID uuid=UUID.randomUUID();

    String str = uuid.toString(); 

    String uuidStr=str.replace("-", "");

    return uuidStr;

   }

2.2 generate the storage file name according to the time to avoid duplicate storage file names

System.nanoTime() 

This function returns nanoseconds. 1 millisecond = 1 nanosecond * 1000 * 1000
For example: long time1 = system. Nanotime();

2.3 or using simpledateformat to format the date into milliseconds can also solve the problem of duplicate file names.

Test.

The open page address is as follows: