如何使用<html:file/>标签实现将本地文件传到服务器上,谢谢了!
解决方案 »
- java.net.ProtocolException: Cannot write output after reading input
- 高人求解,关于<sql:query>标签
- 请问JSP与SERVLET有什么区别?
- 如何防止浏览器右键返回问题?
- 请问JDBC连接数据库之后为什么不能更改SQL语句了?
- 为什么这里却不到content的值呢?
- GWT導入外部類的問題
- javax.servlet.的request问题
- 1个简单JSP问题,进来留言的都有高分。谢谢
- 当使用了PreparedStatement后,该如何改变已经存贮并执行的sql语句?
- 我在本地调试好了网站,传到万网jsp空间上,搞了一晚上都没调试好,有偿请调试,看到速与我联系。
- struts路径问题
import org.apache.struts.upload.FormFile;public class FileUploadForm extends ActionForm {
// Default bean constructor
public FileUploadForm() { } /**
* The file that the user has uploaded
*/
private FormFile file;
public FormFile getFile() { return this.file; }
public void setFile(FormFile file) { this.file = file; } /**
* The name of the file - only for displaying results
*/ private String fname;
public String getFname() { return this.fname; }
public void setFname(String fname) { this.fname = fname; }
/**
* The size of the file - only for displaying results
*/
private String size;
public String getSize() { return this.size; }
public void setSize(String size) { this.size = size; }
}//////////////////////////////////////////////////////////////////////////////
package action;import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;import forms.FileUploadForm;public class FileUploadAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception { String dir = servlet.getServletContext().getRealPath("/upload"); FileUploadForm hff = (FileUploadForm) form; FormFile file = hff.getFile(); if (file == null) {
return mapping.findForward("success"); } // Get the name and file size
String fname = file.getFileName();
String size = Integer.toString(file.getFileSize()) + " bytes"; InputStream streamIn = file.getInputStream();
OutputStream streamOut = new FileOutputStream(dir + "/" + fname); int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
streamOut.write(buffer, 0, bytesRead);
} streamOut.close();
streamIn.close();
// Populate the form bean with the results for display in the View
hff.setFname(fname);
hff.setSize(size); // Clean up our toys when done playing
file.destroy(); // Forward to default display
return mapping.findForward("success"); }}
//////////////////////////////////////////////////////////////////////////////////////
<action path="/HtmlFile"
type="action.FileUploadAction"
name="FileUploadForm"
scope="session"
input="/index.jsp"
validate="false">
<forward name="success" path="/pages/HtmlFile.jsp"/>
</action>
////////////////////////////////////////////////////////////////////////<%@ page language="java" %>
<%@ page import="org.apache.struts.action.*" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html:html>
<head>
<title><html:file> sample code</title>
</head>
<body bgcolor="white"><h1><html:file> sample code</h1>
<!--
The most important part is to declare your form's enctype
to be "multipart/form-data", and to have an html:file
element that maps to your ActionForm's FormFile property
-->
<html:form action="HtmlFile.do" enctype="multipart/form-data"> Please select the file that you would like to upload:<br />
<html:file property="file" /><br /><br /> <html:submit /></html:form><p>
<logic:notEmpty name="FileUploadForm" property="fname" >
The file just uploaded was:<p>
<ul>
<li>Name =<bean:write name="FileUploadForm" property="fname" />
<li>Size =<bean:write name="FileUploadForm" property="size" />
</ul>
</logic:notEmpty></body>
</html:html>
配置下就行了