struts2文件上传
来源:互联网 发布:打骚扰电话软件 编辑:程序博客网 时间:2024/05/22 16:54
jsp页面代码
<form id="bb" name="myform" action="" method="post" enctype="multipart/form-data"> <input id="aa" type="hidden" name="id" value="" /> <table width="800" border="0" cellpadding="0" cellspacing="0" align="center" id="td1"> </table> </form>
js代码
function readyok(id) { var infoPath = $("#knowledgeinfo").val();//文件保存路劲: d:a\b\d\d.rar //alert(infoPath); /*$("#bb").attr("action", "/Examination2.0/upload_uploadfile_hwh.action?filename="+infoPath+"&id=" + id);*/ //tomcat的url地址不允许有"\"字符 $("#bb").attr("action", "/Examination2.0/upload_uploadfile.action?id="+id); if (infoPath == "") { $("#errorInfo").html("请选择您要上传的文件!"); $("#errorInfo").attr("class", "error"); return false; } var fileInfo = $("#knowledgeinfo").val(); var filetype = fileInfo.substring(fileInfo.lastIndexOf(".") + 1); if (filetype != "rar" && filetype != "zip" && filetype != "RAR" && filetype != "ZIP") { $("#errorInfo").html("您上传的文件格式有误,请上传后缀名为.rar或.zip的文件!"); $("#errorInfo").attr("class", "error"); return false; } var fileName = fileInfo.substring(fileInfo.lastIndexOf("\\") + 1); var reg = /^[a-zA-Z0-1][\w.]{1,30}$/; if (!reg.test(fileName)) { $("#errorInfo").html("您上传的文件的文件名有误,请将上传的文件的文件名改为英文!"); $("#errorInfo").attr("class", "error"); return false; } document.myform.submit();}
action代码
package com.yc.webexam.actions;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.List;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;import org.apache.tomcat.util.http.fileupload.FileItem;import org.apache.tomcat.util.http.fileupload.FileUploadException;import org.apache.tomcat.util.http.fileupload.RequestContext;import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;import org.springframework.context.annotation.Scope;import com.opensymphony.xwork2.ActionSupport;import com.yc.biz.ADailyTalkBiz;@SuppressWarnings("serial")@Scope("prototype")public class UploadAction extends ActionSupport { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm"); @Resource(name = "aDailyTalkBiz") private ADailyTalkBiz aDailyTalkBiz; HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); private HttpSession mysession = request.getSession(); public void setServletResponse(HttpServletResponse response) { response.setCharacterEncoding("utf-8"); response.setContentType("text/html; charset=utf-8"); this.response = response; } public void setServletRequest(HttpServletRequest request) { try { request.setCharacterEncoding("utf-8"); this.request = request; this.mysession = this.request.getSession(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } // 封装上传文件域的属性 private File knowledgeinfo; // 封装上传文件类型的属性 private String knowledgeinfoContentType; // 封装上传文件名的属性 private String knowledgeinfoFileName; // 接受依赖注入的属性 private String savePath; /*public void uploadfile_hwh() throws FileUploadException{ DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(1024*1024*80); factory.setRepository(new File(request.getServletContext().getRealPath("WEB-INF/temp"))); //1.2获取上传文件的核心类 ServletFileUpload fileload = new ServletFileUpload(factory); fileload.setHeaderEncoding("UTF-8"); fileload.setFileSizeMax(1024*1024*100); fileload.setSizeMax(1024*1024*200); List<FileItem> list = fileload.parseRequest((RequestContext) request); for(FileItem item :list){ if(!item.isFormField()){//普通字段 String realname = item.getName(); System.out.println("realname======> "+realname); } } }*/ public void uploadfile() { String id = null; // 新技术编号 byte[] bt = null; String fileName = null; int result = 0; id = request.getParameter("id"); FileOutputStream fos = null; FileInputStream fis = null; try { // 建立文件输出流 String filetype = getKnowledgeinfoFileName().substring(getKnowledgeinfoFileName().lastIndexOf(".")); String filename = getKnowledgeinfoFileName().substring(getKnowledgeinfoFileName().lastIndexOf("\\") + 1, getKnowledgeinfoFileName().lastIndexOf(".")) + sdf.format(new Date()) + filetype; String path=getSavePath() ; //获取Tomact根路劲 C:\Users\Administrator\Desktop\apache-tomcat-8.0.44\webapps for(int i=0;i<2;i++){ path=path.substring(0,path.lastIndexOf("\\")); System.out.println(path); } path=path+"\\"+"upload"; String file = path + "\\" + filename; fos = new FileOutputStream(file); // 建立文件上传流 //TODO fis = new FileInputStream(getKnowledgeinfo()); byte[] buffer = new byte[1024]; int len = 0; while ((len = fis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fis = new FileInputStream(file); bt = new byte[fis.available()]; fis.read(bt); fis.close(); result = aDailyTalkBiz.uploadKnowledgeInfos(bt, Integer.parseInt(id), filename); } catch (Exception e) { e.printStackTrace(); } finally { close(fos, fis); } } /** * 返回上传文件的保存位置 * * @return */ public String getSavePath() throws Exception { return ServletActionContext.getServletContext().getRealPath(savePath); } public void setSavePath(String savePath) { this.savePath = savePath; } public File getKnowledgeinfo() { return knowledgeinfo; } public void setKnowledgeinfo(File knowledgeinfo) { this.knowledgeinfo = knowledgeinfo; } public String getKnowledgeinfoContentType() { return knowledgeinfoContentType; } public void setKnowledgeinfoContentType(String knowledgeinfoContentType) { this.knowledgeinfoContentType = knowledgeinfoContentType; } public String getKnowledgeinfoFileName() { return knowledgeinfoFileName; } public void setKnowledgeinfoFileName(String knowledgeinfoFileName) { this.knowledgeinfoFileName = knowledgeinfoFileName; } private void close(FileOutputStream fos, FileInputStream fis) { if (fis != null) { try { fis.close(); } catch (IOException e) { System.out.println("FileInputStream关闭失败"); e.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e) { System.out.println("FileOutputStream关闭失败"); e.printStackTrace(); } } } public void setaDailyTalkBiz(ADailyTalkBiz aDailyTalkBiz) { this.aDailyTalkBiz = aDailyTalkBiz; }}
阅读全文
0 0
- struts2上传文件(三) struts2上传文件
- [Struts2]Struts2的文件上传
- Struts2的文件上传
- Struts2文件上传
- Struts2 上传文件实例
- struts2文件上传
- struts2文件上传
- Struts2的文件上传
- struts2-上传文件(1)
- struts2-上传文件(2)
- Struts2上传文件示例
- struts2文件上传
- Struts2实现文件上传
- struts2文件上传
- Struts2 文件上传
- ext + struts2 上传文件
- Struts2 文件上传
- Struts2的文件上传
- 简单实现贪吃蛇玩法(非智能版还没那水平)
- 获取请求IP地址--一则code review的故事
- 完整页面登录
- redis
- Java动态创建JButton并添加对应监听响应
- struts2文件上传
- 事件类型--焦点事件
- unicode 转 utf8
- SQL一张表中的字段根据另一张表来更新
- 免安装oracle客户端使用PLSQL
- 电视机和机顶盒等焦点问题
- HTML5和CSS编写规范
- 如何用SecureCRT 访问 EC2
- yocto