<b>struts实现多图片上传</b>[Java编程]
本文“<b>struts实现多图片上传</b>[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
package com.ninetowns.zhangc.struts.action;
public class Constant {
private Constant() {
}
public static Integer pictureSize = 10*1024*1024; //单位为byte 上传文件最大限额;
public static String pictureDirectory = "upload\\";
}
package com.ninetowns.zhangc.struts.action;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
import org.apache.struts.upload.MultipartRequestHandler;
public class PictureUpLoad {
private PictureUpLoad() {
}
private static boolean isValidFile(String fileName) {
String[] validFiles = { "gif", "jpg", "jpeg", "jpe", "bmp", "png","txt/plain","text","doc","rar" };
boolean ret = false;
for (int i = 0; i < validFiles.length; i++) {
if (fileName.toLowerCase().endsWith(validFiles[i])) {
ret = true;
break;
}
}
return ret;
}
private static String rebuildFileName(String fileName) {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
"yyyyMMddHHmmss");
java.util.Date date = new java.util.Date();
return sdf.format(date) + "_" + fileName;
}
private static String lianjieFileName(String fileName){
return " "+fileName;
}
private static boolean isDirectoryExists(String path){
java.io.File file = new File(path);
if(!file.exists()){
return file.mkdir();
}else
return true;
}
private static String[] getFileName(String filesName){
Pattern x=Pattern.compile("\\w*\\s+\\w*");
String[] fileName = filesName.split("\\s+");
Matcher t = x.matcher(filesName);
System.out.println( t.matches());
return fileName;
}
public static String[] fileUpLoad(ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String path = "";
String fileName = "";
MultipartRequestHandler handle = form.getMultipartRequestHandler();
Hashtable hashTable = handle.getFileElements();
Collection cln = hashTable.values();
Iterator iterator = cln.iterator();
while (iterator.hasNext()) {
FormFile file = (FormFile) iterator.next();
path = request.getRealPath("/") + Constant.pictureDirectory;
if(isDirectoryExists(path)==false) return null;
if (file.getFileName() == null)
return null;
if (file.getFileSize() > Constant.pictureSize)
return null;
if (!isValidFile(file.getContentType()))
return null;
try {
InputStream in = file.getInputStream();
OutputStream out = new FileOutputStream(path
+ rebuildFileName(file.getFileName()));// "/"表示保存的途径可以改正成"load/"
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
out.write(buffer, 0, bytesRead);
}
out.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
fileName=fileName+lianjieFileName(rebuildFileName(file.getFileName()));
}
return getFileName(fileName);
}
}
package com.ninetowns.zhangc.struts.action;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import org.apache.struts.upload.MultipartRequestHandler;
import com.ninetowns.zhangc.struts.form.UploadForm;
/**
* MyEclipse Struts Creation date: 10-18-2006
*
* XDoclet definition:
*
* @struts.action input="new.jsp" validate="true"
* @struts.action-forward name="list" path="list.jsp" redirect="true"
*/
public class UploadAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
UploadForm updForm = (UploadForm)form;
try {
String[] fileNames = PictureUpLoad.fileUpLoad(updForm, request, response);
request.setAttribute("fileNames", fileNames);
System.out.println("========="+fileNames.length);
for(String file:fileNames){
System.out.println("filename "+file);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mapping.findForward("index");
}
}
package com.ninetowns.zhangc.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
* MyEclipse Struts
* Creation date: 10-18-2006
*
* XDoclet definition:
* @struts.form name="uploadForm"
*/
public class UploadForm extends ActionForm {
/*
* Generated fields
*/
/** name property */
private String name;
/*
* Generated Methods
*/
/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}
/**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
/**
* Returns the name.
* @return String
*/
public String getName() {
return name;
}
/**
* Set the name.
* @param name The name to set
*/
public void setName(String name) {
this.name = name;
}
}
以上是“<b>struts实现多图片上传</b>[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |