Look before you have to understand the SPRINGMVC to lay a good foundation, the following directly to see the effect of the map
Code writing
1. Import Related Rack Package
2. Configuration file
Xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>watermarkspringmvc</display-name>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<context:component-scan base-package="com.wenteryan"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<property name="maxUploadSize" value="10485760000"></property>
<property name="maxInMemorySize" value="40960"></property>
</bean>
</beans>
3. Write action
Watermarkaction. Action
Package COM.WENTERYAN.WATERMARKSPRINGMVC;
Import javax.servlet.http.HttpSession;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Org.springframework.web.bind.annotation.RequestParam;
Import Org.springframework.web.multipart.commons.CommonsMultipartFile;
Import Org.springframework.web.servlet.ModelAndView;
Import Com.wenteryan.service.MarkService;
Import Com.wenteryan.service.UploadService;
@Controller public class Watermarkaction {private Markservice mackservice;
Private Uploadservice Uploadservice; @RequestMapping (value= "/watermark", method=requestmethod.post) public Modelandview watermark (@RequestParam ("image"
Commonsmultipartfile file, HttpSession session) throws Exception {String Uploadpath = "/images"; String Realuploadpath = Session.getservletcontext (). GetrealpatH (Uploadpath);
String imageUrl = uploadservice.uploadimage (file, Uploadpath, Realuploadpath);
String Logoimageurl = Mackservice.watermark (file, Uploadpath, Realuploadpath);
Modelandview ret = new Modelandview ();
Ret.addobject ("ImageUrl", IMAGEURL);
Ret.addobject ("Logoimageurl", Logoimageurl);
Ret.setviewname ("watermark");
return ret;
@Autowired public void Setmackservice (Markservice mackservice) {this.mackservice = Mackservice;
@Autowired public void Setuploadservice (Uploadservice uploadservice) {this.uploadservice = Uploadservice;
}
}
4. Writing Service Classes
Markservice. java
Package com.wenteryan.service;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.io.File;
Import Org.springframework.web.multipart.commons.CommonsMultipartFile;
Public interface Markservice {public
static final String mark_text = "Wenteryan";
public static final String font_name = "Microsoft Ya-hei";
public static final int font_size =;
public static final int font_stype = Font.Bold;
public static final Color font_color = color.red;
public static final int X = ten;
public static final int Y = ten;
public static float ALPHA = 0.3F;
public string watermark (commonsmultipartfile file, String uploadpath,
string realuploadpath);
5. Write interface Implementation class
Uploadservice. java
Package Com.wenteryan.service.impl;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Org.springframework.stereotype.Service;
Import Org.springframework.web.multipart.commons.CommonsMultipartFile; @Service public class Uploadservice {public string uploadimage (commonsmultipartfile file, String Uploadpath, String rea
Luploadpath) {InputStream is = null;
OutputStream OS = null;
try {is = File.getinputstream ();
OS = new FileOutputStream (realuploadpath+ "/" +file.getoriginalfilename ());
byte[] buffer = new byte[1024];
int len = 0;
while ((Len=is.read (buffer)) >0) {os.write (buffer);
} catch (Exception e) {e.printstacktrace ();
finally {if (is!=null) {try {is.close ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
} if (Os!=null) {try {os.close ();
catch (IOException e) { TODO auto-generated Catch block E.printstacktrace ();
}} return uploadpath+ "/" +file.getoriginalfilename ();
}
}
Markserviceimpl. Java
package com.wenteryan.service.impl;
import java.awt.AlphaComposite;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.wenteryan.service.MarkService;
@Service
public class MarkServiceImpl implements MarkService {
@Override
public String watermark(CommonsMultipartFile file, String uploadPath, String realUploadPath) {
// TODO Auto-generated method stub
String logoFileName = "logo"+file.getOriginalFilename() ;
OutputStream os = null ;
try {
Image image2 = ImageIO.read(file.getInputStream()) ;
int width = image2.getWidth(null) ;
int height = image2.getHeight(null) ;
BufferedImage bufferImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB) ;
Graphics2D g = bufferImage.createGraphics() ;
g.drawImage(image2, 0, 0, width, height, null) ;
g.setFont(new Font(FONT_NAME,FONT_STYPE,FONT_SIZE));
g.setColor(FONT_COLOR) ;
int width1 = FONT_SIZE*getTextLength(MARK_TEXT) ;
int height1 = FONT_SIZE ;
int widthDiff = width-width1 ;
int heightDiff = height-height1 ;
int x = X ;
int y = Y ;
if(x>widthDiff) {
x = widthDiff ;
}
if(y>heightDiff) {
y=heightDiff ;
}
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
g.drawString(MARK_TEXT, x, y+FONT_SIZE) ;
g.dispose() ;
os = new FileOutputStream(realUploadPath+"/"+logoFileName) ;
JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os) ;
en.encode(bufferImage) ;
} catch(Exception e) {
e.printStackTrace() ;
} finally {
if(os!=null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return uploadPath+"/"+logoFileName;
}
public int getTextLength(String text) {
int length = text.length();
for(int i=0; i<text.length(); i++) {
String s = String.valueOf(text.charAt(i)) ;
if(s.getBytes().length>1) {
length++ ;
}
}
length = length%2==0?length/2:length/2+1 ;
return length ;
}
}
6. Writing pages
index.jsp
<form action = "watermark" method = "post" enctype = "multipart / form-data">
<h2> Please select the uploaded image </ h2>
<div class = "form-group">
<br>
<input type = "file" name = "image" id = "image" />
</ div>
<div class = "form-group">
<br>
<button class = "btn btn-success" type = "submit"> Start uploading </ button>
</ div>
</ form>
watermark.jsp
<div class="panel-body">
<img class="img-responsive img-rounded" src="${pageContext.request.contextPath}${imageUrl }"/>
<img class="img-responsive img-rounded" src="${pageContext.request.contextPath}${logoImageUrl }"/>
<a class="btn btn-warning" href="${pageContext.request.contextPath }">返回</a>
</div>
Summarize
Java has a special image processing package, the same should be able to realize the watermark function, check the data small try to find a Java watermark is very convenient, watermark can be a picture or text, there will be watermark image Watermark Later, there is a need to write a code batch processing their own pictures.
The above is the entire content of this article, I hope to learn Java program to help you.