<input type="file" name="ss">比如用户选择一张图片,怎样上传到你的文件夹中。
给高分哦!!
给高分哦!!
解决方案 »
- 紧急求救:如何使用jquery在页面中返回本地文件:a.json
- js的问题
- [分享]IE7下propertychange事件引发的栈溢出问题解决
- 这个该如何写?
- 各位帮忙解释一段函数
- javascript的疑惑,绑定select控件的问题
- 菜鸟求个JS求和的问题
- window.open 的问题!!
- 怎样去掉<a href="xx.html">xx.html</a>的<a href="xx.html"></a>,只留下xx.html
- 自做的控件有一方法,返回VARIANT变量类型,在javascript中如何能使用该VARIANT数据?
- 请问window.open打开的新窗口代码里的scrollbars=no覆盖了open里的yes参数怎么办
- 求助简单FOR循环
<body>
<html:form action="/upload" method="POST" enctype="multipart/form-data"><!-- 注意编码 -->
<html:file property="theFile"></html:file><br>
<html:submit>提交</html:submit>
</html:form>
</body>
form:
package com.struts.form;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import javax.servlet.http.HttpServletRequest;
public class UploadForm extends ActionForm{
private FormFile theFile;
public void reset(ActionMapping mapping,HttpServletRequest request){
this.theFile=null;
}
public void setTheFile(FormFile theFile){
this.theFile=theFile;
}
public FormFile getTheFile(){
return this.theFile;
}
}
实现上传的action:
package com.struts.action;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.upload.FormFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.struts.form.UploadForm;
import java.io.FileOutputStream;
import java.io.File;
/*
* 测试struts的上传Action
*/
public class TestUpLoadAction extends Action{
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)throws Exception{
UploadForm uf=(UploadForm)form;
FormFile ff=uf.getTheFile();
HttpSession session=request.getSession();
String fileName=ff.getFileName();//获取名称
int size=ff.getFileSize();
String contentType=ff.getContentType();//获取类型
byte[] data=ff.getFileData();//获取上传内容
session.setAttribute("fileName",fileName);
session.setAttribute("contentType",contentType);
session.setAttribute("size",size);
FileOutputStream fo=new FileOutputStream(new File("e:\\"+fileName));
fo.write(data);
fo.close();
return mapping.findForward("download");
}
}
也可以用jsmart这个jar包内面的类实现.不过我现在没现成的..
文件名:do_upload.jsp
作 者:纵横软件制作中心雨亦奇([email protected])
--%>
<%@ page contentType="text/html; charset=gb2312" language="java"
import="java.util.*,com.jspsmart.upload.*" errorPage="" %>
<html>
<head>
<title>文件上传处理页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head><body>
<%
SmartUpload su = new SmartUpload();
su.initialize(pageContext);
//3.设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
su.setAllowedFilesList("doc,txt");
//jsp,htm,html扩展名的文件和没有扩展名的文件。
su.setDeniedFilesList("exe,bat,jsp,htm,html");
// 上传文件
su.upload();
// 将上传文件全部保存到指定目录
int count = su.save("/upload");
out.println(count+"个文件上传成功!<br>");// 利用Request对象获取参数之值
out.println("TEST="+su.getRequest().getParameter("TEST")
+"<BR><BR>");// 逐一提取上传文件信息,同时可保存文件。
for (int i=0;i<su.getFiles().getCount();i++)
{
com.jspsmart.upload.File file = su.getFiles().getFile(i);// 若文件不存在则继续
if (file.isMissing()) continue;// 显示当前文件信息
out.println("<TABLE BORDER=1>");
out.println("<TR><TD>表单项名(FieldName)</TD><TD>"
+ file.getFieldName() + "</TD></TR>");
out.println("<TR><TD>文件长度(Size)</TD><TD>" +
file.getSize() + "</TD></TR>");
out.println("<TR><TD>文件名(FileName)</TD><TD>"
+ file.getFileName() + "</TD></TR>");
out.println("<TR><TD>文件扩展名(FileExt)</TD><TD>"
+ file.getFileExt() + "</TD></TR>");
out.println("<TR><TD>文件全名(FilePathName)</TD><TD>"
+ file.getFilePathName() + "</TD></TR>");
out.println("</TABLE><BR>");
}
%>
</body>
</html> 这段代码可以帮我看一下不?有500错误!!