- Struts 多图片上传
- 转(http://www.68design.net/Development/JSP/29556-2.html) 我总结了一下收藏了
- upfile.jsp:
- <html:html lang="true">
- <head>
- <html:base />
- <title>upfile.jsp</title>
- <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>
- <html:form action="uploaded.do" enctype="multipart/form-data" method="post">
- <input type="file" name="file0" size="35" value="">
- <input type="file" name="file1" size="35" value="">
- <input type="submit" value="上传文件" value="submit"
- οnclick="return(confirm('你确认要上传文件吗?'))">
- </html:form>
- </body>
- </html:html>
- Action:
- public class UploadedAction extends Action {
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- DynaActionForm fileForm = (DynaActionForm) form;
- FormFile file0 = (FormFile) fileForm.get("file0");
- FormFile file1 = (FormFile) fileForm.get("file1");
- Map< String, FormFile> fileMap = new HashMap< String, FormFile>();
- fileMap.put("file0" + "*type0*name0", file0);
- fileMap.put("file1" + "*type1*names1", file1);
- Set fileSet = fileMap.entrySet();
- Iterator iter = fileSet.iterator();
- String filePath = this.getServlet().getServletContext().getRealPath("/");
- // 保存文件的文件夹
- File savePath = new File(filePath + "UploadFiles//");
- filePath = filePath+ "UploadFiles//";
- if (!savePath.exists()) {
- savePath.mkdir();
- }
- while (iter.hasNext()) {
- Map.Entry unit = (Map.Entry) iter.next();
- String key = (String) unit.getKey();
- FormFile file = (FormFile) unit.getValue();
- //文件大小符合要求,且是图片文件
- if ((file.getFileSize() >= 1)&& DealPhoto.isPhoto(file)) {
- //图片类别
- String photoType = key.substring(key.indexOf("*") + 1, key
- .lastIndexOf("*"));
- System.out.println(photoType);
- //图片描述
- String photoName = key.substring(key.lastIndexOf("*") + 1, key
- .length());
- System.out.println(photoName);
- //存数据库操作,在数据库中保存文件的名称,类型,及在服务器上的相对
- 路径
- //判断是否重名
- if(DealPhoto.isFileExist(file.getFileName(),filePath))
- DealPhoto.rename(file.getFileName(),filePath);
- try {
- InputStream stream = file.getInputStream();// 把文件读入
- // 建立一个上传文件的输出流
- OutputStream bos = new FileOutputStream
- (filePath+file.getFileName());
- int bytesRead = 0;
- byte[] buffer = new byte[stream.available()];
- while ((bytesRead = stream.read(buffer, 0, stream.available()))
- != -1) {
- bos.write(buffer, 0, bytesRead);// 将文件写入服务器
- }
- bos.close();
- stream.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- return mapping.findForward("success");
- }
- }
- Struts-config.xml:
- <struts-config>
- <data-sources />
- <form-beans >
- <form-bean name="uploadForm" type="org.apache.struts.action.DynaActionForm">
- <form-property name="file0" type ="org.apache.struts.upload.FormFile"/>
- <form-property name="file1" type ="org.apache.struts.upload.FormFile"/>
- </form-bean>
- </form-beans>
- <global-exceptions />
- <global-forwards>
- <forward name= "success" path="/test.jsp"></forward>
- </global-forwards>
- <action-mappings >
- <action
- name="uploadForm"
- input="/upfile.jsp"
- path="/uploaded"
- type="com.struts.action.UploadedAction" />
- </action-mappings>
- <message-resources parameter="com.struts.ApplicationResources" />
- </struts-config>
- 辅助类 DealPhoto:
- public class DealPhoto {
- public static boolean isPhoto(FormFile file) {
- String fileName = getString(file.getFileName());
- if (fileName.equals(""))
- return false;
- if ((fileName.toLowerCase().endsWith(".jpg"))
- || (fileName.toLowerCase().endsWith(".gif"))
- || (fileName.toLowerCase().endsWith(".png")))
- return true;
- else
- return false;
- }
- /**
- *
- * @param str
- * @return
- */
- public static String getString(String str) {
- if (str == null)
- str = "";
- if (str.equals("null"))
- str = "";
- str = str.trim();
- return str;
- }
- /**
- * 判断文件是否存在
- * @param fileName
- * @param dir
- * @return
- */
- public static boolean isFileExist(String fileName, String dir) {
- File files = new File(dir + fileName);
- return (files.exists()) ? true : false;
- }
- /**
- * 重命名
- * @param fileName
- * @param dir
- */
- public static void rename(String fileName, String dir) {
- String extendFile = "";
- if (isJpg(fileName))
- extendFile = ".jpg";
- else if (isGif(fileName))
- extendFile = ".gif";
- else if (isPng(fileName))
- extendFile = ".png";
- else
- extendFile = ".jpg";
- Random random = new Random();
- int add = random.nextInt(10000);
- String ret = fileName + add + extendFile;
- while (isFileExist(ret, dir)) {
- add = random.nextInt(10000);
- ret = fileName + add + extendFile;
- }
- File file = new File(dir + fileName);
- File reFile = new File(dir + ret);
- file.renameTo(reFile);
- }
- public static boolean isGif(String file) {
- if (file.toLowerCase().endsWith(".gif")) {
- return true;
- } else {
- return false;
- }
- }
- public static boolean isJpg(String file) {
- if (file.toLowerCase().endsWith(".jpg")) {
- return true;
- } else {
- return false;
- }
- }
- public static boolean isPng(String file) {
- if (file.toLowerCase().endsWith(".png")) {
- return true;
- } else {
- return false;
- }
- }
- }
Struts 多图片上传
最新推荐文章于 2024-11-12 20:35:48 发布