JavaScript Uploadify文件上传实例

java+javascript,没用上数据库,做了一个简简单单的jsp上传小功能,就是记录一下,对于这个小项目有什么建议,欢迎指出不足之处 (-_+)

PS:从我的包名看得出,我用过很多前辈们的代码,基本上都是东凑西拼,各个部分代码的出处就不一一列出了,能看就行!

运行环境:

Java EE + Tomcat 7.0

项目结构
( Dynamic Web Project )

JavaScript Uploadify文件上传实例

src各部分代码

1. com.bijian.study.Upload.java

package com.bijian.study; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; 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.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import com.test.util.FileUtil; @SuppressWarnings("serial") public class Upload extends HttpServlet { private final String SEP = File.separator; private FileUtil fUtil = new FileUtil(); @SuppressWarnings("unchecked") public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String savePath = this.getServletConfig().getServletContext() .getRealPath(""); savePath = savePath + "/uploads/"; File f1 = new File(savePath); System.out.println(savePath); if (!f1.exists()) { f1.mkdirs(); } DiskFileItemFactory fac = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(fac); upload.setHeaderEncoding("utf-8"); String package_path = request.getParameter("save_path"); String remark = request.getParameter("remark"); List fileList = null; try { fileList = upload.parseRequest(request); } catch (FileUploadException ex) { return; } Iterator<FileItem> it = fileList.iterator(); String name = ""; String extName = ""; String sysDate = ""; String fileFrom = ""; while (it.hasNext()) { FileItem item = it.next(); if (!item.isFormField()) { long size = item.getSize(); String type = item.getContentType(); name = item.getName(); if (name == null || name.trim().equals("")) { continue; } if (name.lastIndexOf(".") >= 0) { extName = name.substring(name.lastIndexOf(".")); } File file = null; do { file = new File(savePath + name + extName); } while (file.exists()); File saveFile = new File(savePath + name); fileFrom = saveFile.getAbsolutePath(); Date now = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss"); sysDate = dateFormat.format(now); try { item.write(saveFile); } catch (Exception e) { e.printStackTrace(); } } } remark = ChangeDeocode(remark); package_path = ChangeDeocode(package_path); String keep_path = "C:" +SEP+ package_path+SEP+name; String save_path = "C:" +SEP+ package_path; if(!fUtil.isFileExist(save_path)){ fUtil.mkDir("C:" +SEP+ package_path); } fUtil.copyFile(fileFrom, keep_path); if(fUtil.isFileExist(keep_path)){ fUtil.appendMethod(name + "|" + sysDate + "|" + remark+"|"+save_path); } response.getWriter().print(name + "\\" + sysDate + "\\" + remark); } public String ChangeDeocode(final String dataStr) { String ToDeocode = ""; if (dataStr.contains("%")) { char[] charArray = dataStr.toCharArray(); int ToChangeIndex = 0; for (int i = 0; i < charArray.length; i++) { if (i <= ToChangeIndex && i != 0) { continue; } else { if (charArray[i] == '%' && charArray[i + 1] == 'u' && i + 5 < charArray.length) { char[] tempArray = new char[] { charArray[i], charArray[i + 1], charArray[i + 2], charArray[i + 3], charArray[i + 4], charArray[i + 5] }; ToDeocode += decodeUnicode(String.valueOf(tempArray)); ToChangeIndex = i + 5; } else { ToDeocode += charArray[i]; continue; } } } } return ToDeocode; } public String decodeUnicode(final String dataStr) { int start = 0; int end = 0; final StringBuffer buffer = new StringBuffer(); while (start > -1) { end = dataStr.indexOf("\\u", start + 2); String charStr = ""; if (end == -1) { charStr = dataStr.substring(start + 2, dataStr.length()); } else { charStr = dataStr.substring(start + 2, end); } char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。 buffer.append(new Character(letter).toString()); start = end; } return buffer.toString(); } }

2. com.test.tag.MyIteratorTag.java

package com.test.tag; import java.io.File; import java.io.IOException; import java.util.List; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; public class MyIteratorTag extends TagSupport { private final String SEP = File.separator; private List<String[]> files = null; private List<String[]> data = null; private int columnSize = 0; @Override public int doStartTag() throws JspException { // TODO Auto-generated method stub JspWriter out = this.pageContext.getOut(); StringBuffer sf = new StringBuffer(); sf.append("<table cellspacing='0' cellpadding='10'>"); sf.append("<tr><td>"); sf.append("<table cellspacing='0'>"); if (this.data == null || this.data.isEmpty()) { sf.append(" <tr>"); sf.append(" <td>"); sf.append(" <B> No Valid Data List is NULL </B> "); sf.append(" </td>"); sf.append(" </tr>"); } else { for (int i = 0; i < data.size(); i++) { String[] trs = data.get(i); sf.append("<tr>"); for (int j = 0; j < trs.length; j++) { switch (j) { case 0: sf.append("<td>"); sf.append("<a href='javascript:ReStartJSP("+i+")'>"+trs[j]+"</a>"); sf.append("<input type='hidden' value='"+trs[j]+"'/>"); sf.append("</td>"); break; case 1: sf.append("<td>"); sf.append("\t"+trs[j]); sf.append("</td>"); break; case 2: sf.append("<td>"); sf.append("張"); sf.append("</td>"); break; case 3: sf.append("<td>"); sf.append("<input type='image' value='"+i+"' src='img"+SEP+"exchange_icon.png'/>"); sf.append("</td>"); break; default: sf.append("<td>"); sf.append("<input type='image' value='"+trs[0]+"' src='img"+SEP+"exchange_icon.png'/>"); sf.append("</td>"); break; } } sf.append("</tr>"); } } sf.append("</table>"); sf.append("</td>"); sf.append("<td>"); sf.append("<table cellspacing='0'>"); if(this.files != null && !this.files.isEmpty()){ for (int i = 0; i < files.size(); i++) { String[] trs = files.get(i); sf.append("<tr><u>"); for (int j = 0; j < trs.length; j++) { sf.append("<td>"); sf.append("\t"+trs[j]); sf.append("</td>"); } sf.append("<td>"); sf.append("<input type='image' value='"+trs[1]+"' src='https://www.jb51.net/img/uploadify-cancel.png'/>"); // System.out.println("trs["+i+"]"+trs[i]); sf.append("</td>"); sf.append("</u></tr>"); } } sf.append("</table>"); sf.append("</td></tr>"); sf.append("</table>"); try { out.print(sf.toString()); } catch (IOException e) { e.printStackTrace(); throw new JspException(e.getMessage()); } return super.doEndTag(); } public List<String[]> getfiles() { return files; } public void setfiles(List<String[]> files) { this.files = files; } public List<String[]> getData() { return data; } public void setData(List<String[]> data) { this.data = data; } public int getColumnSize() { return columnSize; } public void setColumnSize(int columnSize) { this.columnSize = columnSize; } }

3. com.test.util.FileStringUtil.java

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwzgxs.html