技术及组件:groovy脚本,gsp,javaee api,apche comments Fileupload
文件上传表单:myIdentiFileForm.gsp
<% def baseDir=request.getContextPath() %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>我的企业认证</title>
<meta charset="UTF-8">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!---->
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div style="font-size: 12px; color: #dddddd;">
位置:会员中心->企业认证
</div>
<h1>
我的企业认证
</h1>
<h5>上传认证文件</h5>
<div style="background-color: #ffffff; border: solid 1px #dddddd; margin: 10px; padding: 10px;" >
<form action="${baseDir}/zh-CN/member/cmpIndentification/action/GvFileUploadAction.groovy" method="post" enctype="multipart/form-data">
<input type="hidden" name="act" value="queryIndentificationUser">
证件类型:<select tname="indType">
<option value="身份证">身份证</option>
<option value="营业执照" selected >营业执照</option>
</select>
<br/> <br/>
证件文件:<input type="file" name="indFile"> *支持pnp,jpg,gif文件类型,大小要求在300K以下。
<br/> <br/>
<input type="submit" value="上传">
</form>
</div>
</body>
</html>
文件上传控制器:GvFileUploadAction.groovy
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import apps.plugs.webapp.core.fileUpload.WebFileUploadUtil
WebFileUploadUtil fileuitl=new WebFileUploadUtil()
//保存文件
def filepath=fileuitl.doPostFile(request)
println "上传文件成功: $filepath "
文件上传处理类:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package apps.plugs.webapp.core.fileUpload;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
*
* @author Administrator
*
* apps.plugs.webapp.core.fileUpload.WebFileUploadUtil
*/
public class WebFileUploadUtil {
@SuppressWarnings("serial")
private String uploadPath = "/data/uploadFile"; // 上传文件的目录
private String tempPath = "/data/uploadFile/buffer/"; // 临时文件目录
File tempPathFile;
@SuppressWarnings("unchecked")
public String doPostFile(HttpServletRequest request)
throws IOException, ServletException {
String uploadfilePath="";
try {
String webDir= request.getSession(true).getServletContext().getRealPath("/");
init(webDir) ;
String uploadPath_real= webDir+this.uploadPath;
String tempPath_real=webDir+this.tempPath;
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Set factory constraints
factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
factory.setRepository(new File(tempPath_real));// 设置缓冲区目录
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Set overall request size constraint
upload.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB
List<FileItem> items = upload.parseRequest(request);// 得到所有的文件
Iterator<FileItem> i = items.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
String fileName = fi.getName();
if (fileName != null) {
File fullFile = new File(fi.getName());
File savedFile = new File(uploadPath_real, fullFile.getName());
fi.write(savedFile);
uploadfilePath=this.uploadPath+fullFile;
System.out.print("upload :"+savedFile.getPath());
}
}
System.out.print("upload succeed");
} catch (Exception e) {
// 可以跳转出错页面
e.printStackTrace();
}
return uploadfilePath;
}
public void init(String webDir) throws ServletException {
String uploadPath= webDir+this.uploadPath;
String tempPath=webDir+this.tempPath;
File uploadFile = new File(uploadPath);
if (!uploadFile.exists()) {
uploadFile.mkdirs();
}
File tempPathFile = new File( tempPath);
if (!tempPathFile.exists()) {
tempPathFile.mkdirs();
}
}
}
第三方文件上传组件库:apache comments-fileupload