Here I record a simple and convenient JAVA upload file picture to the server and save it. The specific content is as follows
First of all, I am submitting a file and type
<div style="border: 1px solid red;"> I added a temporary image to get the media_id on WeChat to save the database! <form action="xxxxxxxxm" enctype="multipart/form-data" method="post"> <div style="display: none;"> <input type="text" value="IMAGE" name="type"/> </div> Upload the image:<input type="file" name="file" onchange="previewImage(this, 'prvid')" multiple="multiple"><br /> <input type="submit" value="Submit" /> </form> <div id="prvid">Preview container</div> </div>
Preview picture js
function previewImage(file, prvid) { /* file: file control* prvid: Image preview container*/ var tip = "Expect jpg or png or gif!"; // Setting prompt message var filters = { "jpeg" : "/9j/4", "gif" : "R0lGOD", "png" : "iVBORw" } var prvbox = document.getElementById(prvid); prvbox.innerHTML = ""; if (window.FileReader) { // html5 scheme for (var i = 0, f; f = file.files[i]; i++) { var fr = new FileReader(); fr.onload = function(e) { var src = e.target.result; if (!validateImg(src)) { alert(tip) } else { showPrvImg(src); } } fr.readAsDataURL(f); } } else { // downgrade processing if (!//.jpg$|/.png$|/.gif$/i.test(file.value)) { alert(tip); } else { showPrvImg(file.value); } } function validateImg(data) { var pos = data.indexOf(",") + 1; for ( var e in filters) { if (data.indexOf(filters[e]) === pos) { return e; } } return null; } function showPrvImg(src) { var img = document.createElement("img"); img.src = src; prvbox.appendChild(img); } } Then it's the background
@RequestMapping(params = "method=addCircle") public String addCircle(HttpServletResponse response,HttpServletRequest request) throws IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String path = request.getSession().getServletContext().getRealPath( "/BackstageShoppingWebsite/images/addCircleimage");//Save server address Map<String, String> map = Upload .upload(request, 1024 * 10, path); String file= map.get("file"); // Name String image = map.get("type"); // Image String newFile = map.get("newFile");// Address return null; } Okay, the focus is now on the Upload class. This class is basically encapsulated. You can take and modify the things you need to add. Then this class uses the cos.jar package.
package com.web.weixin.bean; import java.io.File; import java.io.IOException; import java.util.Date; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import com.oreilly.servlet.multipart.FilePart; import com.oreilly.servlet.multipart.MultipartParser; import com.oreilly.servlet.multipart.ParamPart; import com.oreilly.servlet.multipart.Part; public class Upload { public static Map<String, String> upload(HttpServletRequest request, int maxSize, String path) { //Save data in map form, corresponding to the name name value on the obtain interface, saving the value corresponding to the name on the interface, Map<String, String> map = new HashMap<String, String>(); Part part = null; try { MultipartParser mrequest = new MultipartParser(request, maxSize); mrequest.setEncoding("utf-8"); //Transfuse all part groups while ((part = mrequest.readNextPart()) != null) { if (part.isFile()) { //Judge whether it is a file FilePart filepart = (FilePart) part;//Convert to file group String fileName = filepart.getFileName();//Get the file name if (fileName != null && fileName.length() > 0) { //Get the extension String fileExtName = fileName.substring( fileName.lastIndexOf(".") + 1).toLowerCase(); // Upload only the image // Determine whether the format of the image upload meets the suffix name is valid if (fileExtName.equalsIgnoreCase("jpeg") || fileExtName.equalsIgnoreCase("png") || fileExtName.equalsIgnoreCase("jpg") || fileExtName.equalsIgnoreCase("gif") || fileExtName.equalsIgnoreCase("gif") || fileExtName.equalsIgnoreCase("ico") || fileExtName.equalsIgnoreCase("bmp") || fileExtName.equalsIgnoreCase("flv") || fileExtName.equalsIgnoreCase("mp4") || fileExtName.equalsIgnoreCase("mp3")) { /*String newFileName = new Date().getTime() + "."+ fileExtName; //Re-change file name File name + extension*/ String newFileName = new Date().getTime() + fileName;//Don't change the image name String newPath = path + "/" + newFileName; //Fileprocessing path File upload newFile = new File(newPath); filepart.writeTo(newFile); //Real write the file to the corresponding folder//filepart.getName() Get the name of the parameter to be received by request map.put(filepart.getName(), newFileName); //Save the file information in map map.put("newFile", newFile.toString()); } else { map.put("geshi", "geshi"); continue; }// It means that the upload is not a picture} else { map.put("yes","yes"); continue; // It means that the uploaded picture is not a picture} } else if (part.isParam()) { //Judge whether it is a parameter ParamPart paramPart = (ParamPart) part; map.put(paramPart.getName(), paramPart.getStringValue()); } } } catch (IOException e) { e.printStackTrace(); } return map; } }Download the cos.jar package, click to open the link
This article has been compiled into "Summary of Java Upload Operation Techniques", and everyone is welcome to learn and read.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.