当前位置: 移动技术网 > IT编程>开发语言>Java > java组件smartupload实现上传文件功能

java组件smartupload实现上传文件功能

2019年07月22日  | 移动技术网IT编程  | 我要评论

使用jsp和serlvet来实现最简单的上传,供大家参考,具体内容如下

1、页面index.jsp

<%@ page language="java" pageencoding="utf-8"%> 
<% 
string path = request.getcontextpath(); 
string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; 
%> 
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"> 
<html> 
 <head> 
 <base href="<%=basepath%>"> 
 <title>index.jsp</title> 
 <meta http-equiv="pragma" content="no-cache"> 
 <meta http-equiv="cache-control" content="no-cache"> 
 <meta http-equiv="expires" content="0"> 
 </head> 
 <body> 
 <form action="upload" method="post" enctype="multipart/form-data"> 
 <br> 
 姓名:<input type="text" name="uname"/> 
 <br> 
 上传文件:<input type="file" name="pic"/> 
 <br> 
 <input type="submit" value="提交"></input> 
 </form> 
 </body> 
</html> 

2、action跳转到了upload的servlet,所以要web.xml里面配置,web.xml:

<?xml version="1.0" encoding="utf-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 
 xsi:schemalocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
 <servlet> 
 <servlet-name>uploadaction</servlet-name> 
 <servlet-class>com.pop.action.smartuploadaction</servlet-class> 
 </servlet> 
 <servlet-mapping> 
 <servlet-name>uploadaction</servlet-name> 
 <url-pattern>/upload/*</url-pattern> 
 </servlet-mapping> 
 <welcome-file-list> 
 <welcome-file>index.jsp</welcome-file> 
 </welcome-file-list> 
</web-app> 

3、映射到的action文件,smartuploadaction.java:

package com.pop.action; 
 
import java.io.ioexception; 
import javax.servlet.servletexception; 
import javax.servlet.http.httpservlet; 
import javax.servlet.http.httpservletrequest; 
import javax.servlet.http.httpservletresponse; 
import javax.servlet.jsp.jspfactory; 
import javax.servlet.jsp.pagecontext; 
 
import com.soft4j.httpupload4j.request; 
import com.soft4j.httpupload4j.smartupload; 
import com.soft4j.httpupload4j.smartuploadexception; 
 
public class smartuploadaction extends httpservlet { 
 private static final long serialversionuid = -8610555375032925108l; 
 @override 
 protected void dopost(httpservletrequest req, httpservletresponse resp) 
  throws servletexception, ioexception { 
 req.setcharacterencoding("utf-8"); 
 resp.setcharacterencoding("utf-8"); 
 smartupload su = new smartupload(); 
 // 由于multipart/form-data的传输原因导致req不能使用,所以使用smartupload产生的request 
 request reqest = su.getrequest(); 
 // 获得pagecontext对象 
 pagecontext pagecontext = jspfactory.getdefaultfactory() 
  .getpagecontext(this, req, resp, null, true, 8192, true); 
 su.initialize(pagecontext); 
 try { 
  su.upload(); 
  // 上传到本项目的upload目录 
  su.save("upload"); 
 } catch (smartuploadexception e) { 
  e.printstacktrace(); 
 } 
 // 使用smartupload产生的reqest对象来获得页面传递的参数 
 string uname = reqest.getparameter("uname"); 
 system.out.println(uname); 
 } 
} 

最后说明:使用的组件包为smartupload.zip。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网