标签:name public leo www output lan this 图片 add
package web; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class UploadDemoAction extends ActionSupport { private String username; private File myfile; private String myfileContentType; private String myfileFileName; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public File getMyfile() { return myfile; } public void setMyfile(File myfile) { this.myfile = myfile; } public String getMyfileContentType() { return myfileContentType; } public void setMyfileContentType(String myfileContentType) { this.myfileContentType = myfileContentType; } public String getMyfileFileName() { return myfileFileName; } public void setMyfileFileName(String myfileFileName) { this.myfileFileName = myfileFileName; } public String upload(){ try { String path = ServletActionContext.getServletContext().getRealPath( "/upload/file"); System.out.println("path========"+path); InputStream input = new FileInputStream(myfile); System.out.println("myfileFileName===="+myfileFileName); String allpath=path+"\\"+myfileFileName; System.out.println("allpath===="+allpath); OutputStream output = new FileOutputStream(allpath); IOUtils.copy(input, output); return SUCCESS; } catch (Exception e) { e.printStackTrace(); return INPUT; } } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Insert title here</title> </head> <body> <form action="upload.action" method="post" enctype="multipart/form-data"> 用户名:<input type="text" name="username"/> 文件:<input type="file" name="myfile" /> <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//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <!-- 忽略struts格式 --> <constant name="struts.ui.theme" value="simple"></constant> <constant name="struts.configuration.xml.reload" value="true"></constant> <package name="houseuser" extends="struts-default" namespace="/"> <!-- 上传图片示例--> <action name="upload" class="web.UploadDemoAction" method="upload"> <result >success.jsp</result> </action> </package> </struts>
标签:name public leo www output lan this 图片 add
原文地址:http://www.cnblogs.com/sincoolvip/p/6106515.html