今天从慕课网学了如何用java在图片上添加水印(包括文字水印和图片水印),自己学着写了一遍理解了下,这里先写个简单的文件上传,往后再开始写水印功能。(也可以去慕课网看相应视频,链接:http://www.imooc.com/learn/482)。
环境:struts2环境。
一、单文件上传。
1、文件上传界面主要代码:index.jsp:
<body>
<h4>上传图片</h4>
<hr />
<form name="uploadFile" action="${pageContext.request.contextPath }/waterMark.action" method="post" enctype="multipart/form-data">
<input type="file" name="image"/><br />
<input type="submit" name="uploadImage" value="上传图片"/>
</form>
</body>
2、配置struts.xml文件:
<package name="default" extends="struts-default">
<action name="waterMark" class="com.wjl.watermark.WaterMarkAction" method="waterMark">
<param name="uploadPath">/images</param>
<result name="success">waterMark.jsp</result>
</action>
</package>
3、编写Action代码:
a、集中处理文件上传的WaterMarkAction:
package com.wjl.watermark;
import java.io.File;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class WaterMarkAction extends ActionSupport{
private File image;//上传的文件(注意,file并不是指前端jsp上传过来的文件本身,而是文件上传过来存放在临时文件夹下面的文件)
private String imageFileName;//提交过来的file的名字
private String uploadPath;//文件上传的路径(在struts.xml文件中进行配置)
private PicInfo pic = new PicInfo();//上传后的文件对象
public String waterMark() throws Exception{
String realUploadPath = ServletActionContext.getServletContext().getRealPath(uploadPath);
//上传图片
UploadService uploadService = new UploadService();
pic.setImageURL(uploadService.uploadImage(image, imageFileName, uploadPath, realUploadPath));
return SUCCESS;
}
//image、imageFileName、uploadPath、pic的get/set方法
}
b、进行文件上传的UploadService:
package com.wjl.watermark;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class UploadService {
/**
* 该方法用来上传文件
* @param image:上传的图片对象
* @param imageFileName:上传的文件名称
* @param uploadPath:上传文件的相对路径
* @param realUploadPath:上传文件的绝对路径
* */
public String uploadImage(File image,String imageFileName,String uploadPath,String realUploadPath){
InputStream is =null;
OutputStream os = null;
try {
is = new FileInputStream(image);
os = new FileOutputStream(realUploadPath+File.separator+imageFileName);
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) {
e.printStackTrace();
}
}
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//返回上传后的文件绝对路径
return uploadPath+File.separator+imageFileName;
}
}
c、上传后的文件信息PicInfo:
package com.wjl.watermark;
public class PicInfo {
private String imageURL;
private String logoImageURL;
//两个属性的get/set方法
}
4、图片上传后的页面展示waterMark.jsp:
先引入s标签:<%@ taglib uri="/struts-tags" prefix="s"%>
<body>
<table width="99%" align="center">
<tr>
<td width="50%">
<img src=${pageContext.request.contextPath }<s:property value="pic.imageURL"/> width="350" >
</td>
<td width="50%">
<img src=${pageContext.request.contextPath }<s:property value="pic.logoImageURL"/> width="350" >
</td>
</tr>
</table>
</body>
5、在webRoot目录下新建images目录,启动服务器进行测试。
二、多文件上传。
1、文件上传界面主要代码:index.jsp。
<body>
<h4>上传图片</h4>
<hr />
<form name="uploadFile" action="${pageContext.request.contextPath }/waterMark.action" method="post" enctype="multipart/form-data">
<input type="file" name="image"/><br />
<input type="file" name="image"/><br />
<input type="file" name="image"/><br />
<input type="file" name="image"/><br />
<input type="file" name="image"/><br />
<input type="file" name="image"/><br />
<input type="file" name="image"/><br />
<input type="submit" name="uploadImage" value="上传图片"/>
</form>
</body>
2、struts.xml配置文件与单文件上传一致。
3、用来进行文件上传的UploadService和记录上传后的文件PicInfo与单文件上传相同,集中处理的WaterMarkAction主要修改如下:
package com.wjl.watermark;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class WaterMarkAction extends ActionSupport{
private File[] image;
private String[] imageFileName;
private String uploadPath;
private List<PicInfo> picInfo = new ArrayList<PicInfo>();
public String waterMark() throws Exception{
String realUploadPath = ServletActionContext.getServletContext().getRealPath(uploadPath);
if(image!=null && image.length>0){
PicInfo pic=null;
UploadService uploadService = new UploadService();
for(int i=0;i<image.length;i++){
pic = new PicInfo();
//上传图片
pic.setImageURL(uploadService.uploadImage(image[i], imageFileName[i], uploadPath, realUploadPath));
picInfo.add(pic);
}
}
return SUCCESS;
}
//image、imageFileName、uploadPath、pic的get/set方法
}
4、图片上传后的页面展示waterMark.jsp:
记得引入s标签:<%@ taglib uri="/struts-tags" prefix="s"%>
<body>
<table width="99%" align="center">
<s:iterator value="picInfo">
<tr>
<td width="50%">
<img src=${pageContext.request.contextPath }<s:property value="imageURL"/> width="350" >
</td>
<td width="50%">
<img src=${pageContext.request.contextPath }<s:property value="logoImageURL"/> width="350" >
</td>
</tr>
</s:iterator>
</table>
</body>
5、进行测试。
注意点:
1、文件上传之后记得关闭各种流,避免出现乱七八糟的问题。
2、进行文件上传的form表单一定要设置enctype="multipart/form-data";
3、WaterMarkAction中的File image中的image变量必须与index.jsp中type="file"类型的input框name值相同,否则无法对应上。
4、WaterMarkAction中的imageFileName不是我们自己配置的而是由Struts自带的,它遵循的原则是:前台file控件的name名称+"FileName"。我这里file控件的name是image,所以文件名称我定义imageFileName。若控件名称为file那么则应该定义成fileFileName,file的MIME类型也遵循同样的原则。否则获取不到文件名称。
5、用来记录上传后的文件信息的PicInfo对象中的两个属性一定要给get/set方法,否则在waterMark.jsp页面中无法直接使用其属性。
6、picInfo这个对象给定的get/set方法一定要是getPicInfo/setPicInfo,要是弄成getPic/setPic,那么在waterMark.jsp页面中直接使用imageURL和logoImageURL就会获取不到值。(我会犯这个错就是因为原本picInfo的名称是pic后来改成picInfo的,但是get/set方法没有变化所以就没获取到。)