This case is through the Springmvc+spring+mybatis framework to the product upload as an example, the implementation of the image upload function, and the image to save the address to the database and display the uploaded pictures in the foreground.
This project is a project built using MAVEN, first look at the project structure
Related configuration self-search, directly below to implement the upload function
1. Create a database
DROP TABLE IF EXISTS ' product '; CREATE TABLE ' product ' ( ' pid ' int (one) not null auto_increment, ' pimage ' varchar (255) DEFAULT NULL, PRIMARY K EY (' pid ')) Engine=innodb auto_increment=6 DEFAULT Charset=utf8;--------------------------------Records of product--- ---------------------------INSERT INTO ' product ' VALUES (' 2 ', ' 6c648d82-dc29-4b92-855e-491741e092a21.jpg '); insert Into ' Product ' values (' 3 ', ' 80f26905-7342-492c-be6e-c3f0ad81c2aa1.jpg '); INSERT into ' product ' values (' 4 ', ' C3d28f16-4b17-4568-8877-ff1fd4e514a31.jpg '); INSERT into ' Product ' VALUES (' 5 ', ' BB8070E8-5B3F-4BE2-83D6-698DD6169DCA ');
2. Create commodity entity class product
Public classProduct {PrivateInteger pid; PrivateString Pimage; PublicInteger getpid () {returnpid; } Public voidsetpid (Integer pid) { This. PID =pid; } PublicString getpimage () {returnPimage; } Public voidsetpimage (String pimage) { This. Pimage =Pimage; } @Override PublicString toString () {return"Product [pid=" + pid + ", pimage=" + Pimage + "]"; }
3. Create Productcontroller
@Controller Public classProductcontroller {//Inject Productservice@AutowiredPrivateProductservice Productservice; //Query All Users@RequestMapping ("/list.do") PublicString Listuser (model model) {List<Product> list=productservice.list (); Model.addattribute ("List", list); SYSTEM.OUT.PRINTLN (list); return"List"; } /*** Save Item *@paramimage *@paramProduct *@paramMap *@return * @throwsIOException*/@RequestMapping ("/addproduct.do") PublicString fileUpload (multipartfile file,product Product, Modelmap map)throwsIOException {/*** Upload Images*/ //once the image has been uploaded successfully, write the address of the image to the databaseString FilePath = "E:\\upload";//path to save picture//get the extension name of the original pictureString OriginalFilename =File.getoriginalfilename (); //the new file nameString NewFileName = uuid.randomuuid () +OriginalFilename;
Full path file targetfile that encapsulates the location of the uploaded files=NewFile (filepath,newfilename);
Upload the local file to the full path File.transferto (targetfile) that encapsulates the upload file location; Product.setpimage (NewFileName); /*** Save Items*/Productservice.save (product); return"Redirect:/list.do"; }}
4. Create an interface Productservice
Package Com.ssm.service; Import java.util.List; Import com.ssm.entity.Product; Public Interface productservice { list<Product> list (); void Save (product product);}
5. Create an implementation class Productserviceimpl
@Service @transactional public class productserviceimpl implements Productservice { // Inject productmapper @Autowired private Productmapper Productmapper; @Override public list<product> List () { return Productmapper.list (); @Override public void Save (product product) {Productmapper.save (product); }}
6. Create the Mapper interface
Public Interface productmapper { // Save product void Save (product product); // Product Enquiry list<product> list ();}
7. Create Mapper.xml
<mapper namespace= "Com.ssm.mapper.ProductMapper" > <!--add- <insert id= "Save" Parametertype= "Com.ssm.entity.Product" > INSERT INTO Product (pimage) VALUES (#{pimage}) </ Insert> <!--query users- <select id= "list" resulttype= "Com.ssm.entity.Product" > * From product </select> </mapper>
8. Home index.jsp
<Body> <formAction= "Addproduct.do"Method= "POST"enctype= "Multipart/form-data">Photo:<inputtype= "File"name= "File"> <inputtype= "Submit"value= "Submit"> </form></Body>
9. Display list list.jsp
<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><%@ taglib Prefix="C"URI="Http://java.sun.com/jsp/jstl/core" %><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><Linkrel= "stylesheet"href= "Https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"/><Scriptsrc= "Https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></Script><title>Insert Title here</title><styletype= "Text/css">#images{width:50px;Height:50px; }</style></Head><Body> <Tableclass= "Table table-bordered table-hover"> <TR> <th>Serial number</th> <th>Image</th> </TR> <C:foreachItems= "${list}"var= "Product" > <TR> <th>${product.pid}</th> <th><c:ifTest= "${product.pimage!=null}"> <imgID= "Images"alt=""src= "/image/${product.pimage}"> </c:if> </th> </TR> </C:foreach> </Table></Body></HTML>
Finally, the location of my image upload is saved locally, and I am setting it through Tomcat, such as:
The picture upload process is complete.
DROP TABLE IF EXISTS ' product '; CREATE TABLE ' product ' (' pid ' int (one) not null auto_increment, ' pimage ' varchar (255) DEFAULT NULL, PRIMARY KEY (' pid ') ) Engine=innodb auto_increment=6 DEFAULT Charset=utf8;
--------------------------------Records of product------------------------------INSERT into ' product ' VALUES (' 2 ', ' 6c648d82-dc29-4b92-855e-491741e092a21.jpg '); INSERT into ' Product ' VALUES (' 3 ', ' 80f26905-7342-492c-be6e-c3f0ad81c2aa1.jpg '); INSERT into ' Product ' VALUES (' 4 ', ' C3d28f16-4b17-4568-8877-ff1fd4e514a31.jpg '); INSERT into ' Product ' VALUES (' 5 ', ' BB8070E8-5B3F-4BE2-83D6-698DD6169DCA ');
SSM Framework implements picture upload display and save address to database