Struts2 上传图片
上传工具:UploadFileUtilsimport java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;import javax.servlet.http.HttpServletRequest;public class UploadFileUtils {//限制文件类型private static String[] fileTypes = {"image/bmp","image/png","image/gif","image/jpeg","image/jpg","image/pjpeg","image/pjpg","image/x-png"};//限制文件大小private static long fileSize = 1024*1024L;/** * 根据上传文件获取其真实读取路径 * @param request * @param file * @param fileName * @param path * @return * @throws IOException */public static String setUploadFile(HttpServletRequest request,File file,String fileName,String path) throws IOException{final Lock lock = new ReentrantLock();String newName = null;lock.lock();try {//加锁为防止文件名重复 newName = System.currentTimeMillis()+ fileName.substring(fileName.lastIndexOf("."),fileName.length());}finally {lock.unlock();}//------------ 锁结束 -------------//获取文件输出流 FileOutputStream fos = new FileOutputStream(request.getSession().getServletContext().getRealPath("/")+ path.replaceAll("/", "\\\\") + "\\" + newName);//设置 KE 中的图片文件地址 String newFileName = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ request.getContextPath() + "/"+path+"/" + newName;byte[] buffer = new byte;//获取内存中当前文件输入流 InputStream in = new FileInputStream(file);try {int num = 0;while ((num = in.read(buffer)) > 0) {fos.write(buffer, 0, num);}} catch (Exception e) {e.printStackTrace(System.err);} finally {in.close();fos.close();}return newFileName;}/** * 验证文件类型是否为图片 * @param fileType * @return */public static boolean checkFileImg(String fileType){boolean flag = false;for(String type:fileTypes){if(type.equals(fileType))flag = true;}return flag;}/** * 判断文件是否超长 * @param file * @return */public static boolean checkFileSize(File file){boolean flag = false;long size = file.length();if(size<=fileSize)flag = true;return flag;}//public static void main(String[] args) {//System.out.println("aa/bb/cc".replaceAll("/", "\\\\"));//}}
对应的jsp中上传图片的form
注意form的enctype
<form action="<%=request.getContextPath()%>/modfiyHead.action" method="post" enctype="multipart/form-data"> <a class="close"></a><strong>照片:<input type="file" name="headPicture" value=""/></strong><p>注意:严禁上传****等违法照片。为保证清晰度,建议图片尺寸为80x80。</p><p></p><p></p><p class="btn w1 c"><a >上传</a></p></form>
在action中,要定义三个属性,注意此三个属性,必须以jsp中的<input>的名称一致
// 头像图片private File headPicture;private String headPictureContentType = "";private String headPictureFileName = "";
上传方法:
if(!UploadFileUtils.checkFileImg(headPictureContentType)){setRequestAttr("modfiyheadMSG", "图片类型错误。");return personalInt();}if(!UploadFileUtils.checkFileSize(headPicture)){setRequestAttr("modfiyheadMSG", "图片太大。");return personalInt();}//调用上传工具上传图片String newFileName = UploadFileUtils.setUploadFile(getRequest(),headPicture, headPictureFileName,Constants.HEADPATH);ClubUser user = getSessionUser();user.setUimg(newFileName);userMng.update(user);
页:
[1]