首页 > 代码库 > struts2上传文件
struts2上传文件
1.upload.java(java文件上传的读写方法)
package com.OS.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import org.apache.struts2.ServletActionContext; import com.sql.bean.FILE; import com.opensymphony.xwork2.ActionSupport; public class upload extends ActionSupport { private File picture; private String pictureContentType; private String pictureFileName; FILE file=new FILE(); public FILE upload() throws Exception{ //最好保护文件时重命名加时间截 File saved = new File(ServletActionContext.getServletContext().getRealPath("upload"),pictureFileName); InputStream ins=null; OutputStream ous=null; try{ saved.getParentFile().mkdirs(); ins=new FileInputStream(picture); ous=new FileOutputStream(saved); byte[] b= new byte[1024]; int len =0; while((len=ins.read(b))!=-1){ ous.write(b,0,len); } }catch(Exception e){ e.printStackTrace(); }finally{ if(ous!=null) ous.close(); if(ins!=null) ins.close(); } file.setFile(picture); file.setFileContentType(pictureContentType); file.setFileFileName(pictureFileName); // BufferedReader bf = new BufferedReader(new StringReader(str)); return file; } public File getPicture() { return picture; } public void setPicture(File picture) { this.picture = picture; } public String getPictureContentType() { return pictureContentType; } public void setPictureContentType(String pictureContentType) { this.pictureContentType = pictureContentType; } public String getPictureFileName() { return pictureFileName; } public void setPictureFileName(String pictureFileName) { this.pictureFileName = pictureFileName; } }
2.FILE.java (文件上传类)
package com.sql.bean; import java.io.File; public class FILE { private File file; private String fileContentType; private String fileFileName; public File getFile() { return file; } public void setFile(File file) { this.file = file; } public String getFileContentType() { return fileContentType; } public void setFileContentType(String fileContentType) { this.fileContentType = fileContentType; } public String getFileFileName() { return fileFileName; } public void setFileFileName(String fileFileName) { this.fileFileName = fileFileName; } }
3.uploads.java (action方法)
package com.web.actoin; import org.apache.struts2.ServletActionContext; import com.OS.util.upload; import com.opensymphony.xwork2.ActionSupport; public class uploads extends ActionSupport{ public upload u=new upload(); public String uploadimage() throws Exception{ System.out.println(".."+ServletActionContext.getRequest().getContextPath()+"/upload/"+u.upload().getFileFileName()); return "success"; } public upload getU() { return u; } public void setU(upload u) { this.u = u; } }
4.gMessages.properties(消息文件)
struts.messages.error.uploading = \u6587\u4EF6\u4E0D\u80FD\u4E0A\u4F20\u7684\u901A\u7528\u9519\u8BEF\u4FE1\u606F struts.messages.error.file.too.large = \u4E0A\u4F20\u6587\u4EF6\u957F\u5EA6\u8FC7\u5927\u7684\u9519\u8BEF\u4FE1\u606F struts.messages.error.content.type.not.allowed =\u5F53\u4E0A\u4F20\u6587\u4EF6\u4E0D\u7B26\u5408\u6307\u5B9A\u7684contentType
5.struts.properties(struts2资源文件)
struts.custom.i18n.resources=gMessages
6.struts.xml(struts2配置文件)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.i18n.encoding" value=http://www.mamicode.com/"UTF-8">>
7.index.jsp<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <base href=http://www.mamicode.com/"">>struts2上传文件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。