Recently there is a function to upload pictures, and then realize the picture preview, because the project is older, at the same time the interface and user experience also requires not too high, did not go to find the online open source Plug-ins, directly wrote a simple function, here to do a record backup
Because this is an experimental small code, there is no too much validation and interface adjustment
can be implemented in random order deletion
This is the simple function interface, the following is the code
The first is that SPRINGMVC XM needs to be configured under
<!--configuration multipartresolver for file uploads using spring's commosmultipartresolver Description: p:defaultencoding= "UTF-8": Here the default file encoding is set to UTF-8 Must be consistent with the default encoding of the user's JSP;
p:maxuploadsize= "5000000": Specify file Upload size in bytes; p:uploadtempdir= "fileupload/temp": File Upload temp directory, When the upload is complete, the temporary files will be deleted;-->
<bean id= "multipartresolver"
class= " Org.springframework.web.multipart.commons.CommonsMultipartResolver "
p:defaultencoding=" UTF-8 "P: Maxuploadsize= "5000000" p:uploadtempdir= "fileupload/temp" >
</bean>
Jsp+js
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <% String Path = Request.getcontextpath ()
;
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
Response.setheader ("Cache-control", "No-store");//http 1.1 Response.setheader ("Pragma", "No-cache");//http 1.0 Response.setdateheader ("Expires", 0);//prevents caching at the proxy server%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Tool class
Package com.lovo.utils;
Import Java.util.Arrays;
Import java.util.List;
public class Fileuploadcheck {
//Supported file types public
static final list<string> allow_types = Arrays.aslist (" Image/jpg "," Image/jpeg "," Image/png "," image/gif ");
Verify that the file type is a permitted public
static Boolean allowupload (String postfix) {return
allow_types.contains (postfix);
}
}