标签:login 上传 需要 work 加载 设置 lob 1.2 byte
package com.djoker.struts2; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; 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.opensymphony.xwork2.Action; public class uploadAction { private String myFileFileName; private File myFile; private String descContextType; public String getMyFileFileName() { return myFileFileName; } public void setMyFileFileName(String myFileFileName) { this.myFileFileName = myFileFileName; } public File getMyFile() { return myFile; } public void setMyFile(File myFile) { this.myFile = myFile; } public String getDescContextType() { return descContextType; } public void setDescContextType(String descContextType) { this.descContextType = descContextType; } public String execute() throws Exception { System.out.println(myFileFileName); InputStream is = null; OutputStream os = null; try{ is = new BufferedInputStream(new FileInputStream(myFile)); os = new BufferedOutputStream(new FileOutputStream(ServletActionContext.getServletContext().getRealPath("upload") + "/" + myFileFileName)); byte[] ByteBuffer = new byte[1024]; int len = 0; while((len = is.read(ByteBuffer)) > 0){ os.write(ByteBuffer, 0, len); } } finally { if(is != null){ is.close(); } if(os != null){ os.close(); } } return Action.SUCCESS; } }
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Insert title here</title> </head> <body> <form action="uploadAction.action" method="post" enctype="multipart/form-data"> 选择文件:<input type="file" name="myFile"><br> 文件描述:<input type="text" name="desc"><br> <input type="submit" value="上传"> </form> </body> </html>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <!-- 当struts.xml配置文件发生修改,会立刻加载,在生产环境下最好不要配置 --> <constant name="struts.configuration.xml.reload" value="true"></constant> <!-- 提供更加友好的提示信息 --> <constant name="struts.devMode" value="true"></constant> <!-- 对字符集的设置 --> <constant name="struts.i18n.encoding" value="GB18030"/> <!-- 配置文件上传最大限制 --> <constant name="struts.multipart.maxSize" value="9999999999"></constant> <package name="struts2" extends="struts-default" > <global-results> <result>/success.jsp</result> <result name="error">/error.jsp</result> </global-results> <action name="login" class="com.djoker.struts2.LoginAction"> <result>/success.jsp</result> <result name="error">/error.jsp</result> </action> <action name="uploadAction" class="com.djoker.struts2.uploadAction"> <result>/success.jsp</result> </action> </package> <include file="struts-user.xml"></include> </struts>
标签:login 上传 需要 work 加载 设置 lob 1.2 byte
原文地址:http://www.cnblogs.com/djoker/p/6218789.html