关于jsp的问题 如何在jsp的页面中上传文件,最好能给出具体的实例! 再说说原理也好!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>选择要上传的文件</title> </head> <body> <form action="UploadServlet" method="POST" enctype="multipart/form-data"> <h3> 设置目标文件位置:<input type="text" name="PATH" value="" size="30" /><br> 请选择要上传文件:<input type="file" name="FILE1" value="" width="30" /><br> <input type="submit" value="上传" /> </h3> </form> </body></html>/* * To change this template, choose Tools | Templates * and open the template in the editor. */package com.netbeans.web;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.jspsmart.upload.*;import javax.servlet.ServletConfig;/** * * @author Administrator */public class UploadServlet extends HttpServlet { private ServletConfig config=null; /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ final public void init(ServletConfig config)throws ServletException{ this.config=config; } protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=GBK"); PrintWriter out = response.getWriter(); int count=0; SmartUpload myUpload=new SmartUpload(); try { /* TODO output your page here*/ out.println("<html>"); out.println("<head>"); out.println("<title>Servlet UploadServlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<h3>"); myUpload.initialize(config, request, response); myUpload.upload(); String path=myUpload.getRequest().getParameter("PATH"); count=myUpload.save(path); out.println("已经上传"+count+"个文件<br>"); out.println("已经上传文件路径"+path); }catch(Exception e){ out.println("错误:"+e.toString()); } finally { out.println("</h3></body>"); out.println("</html>"); out.close(); } } // <editor-fold defaultstate="collapsed" desc="HttpServlet 方法。单击左侧的 + 号以编辑代码。"> /** * Handles the HTTP <code>GET</code> method. * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP <code>POST</code> method. * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. * @return a String containing servlet description */ public String getServletInfo() { return "Short description"; }// </editor-fold>}jspsmart组件到网上下载.很多...一般小于2M的文件用jspsmart效果比较好 因为以前做过这样的例子我就要上传图片为例说明一下<script>function f_onchange(i){ document.getElementById("img"+i).src=document.getElementById("file"+i).value; document.getElementById("div"+i).style.display="block";}</script><form name="form1" enctype="multipart/form-data" method="post" action="<%=request.getContextPath()%>/Action/PhotoAction.jsp"> <div class="fileDiv"><input onchange="f_onchange(1)" type="FILE" name="photo" size=40 id="file1"></div> <div class="fileDiv"><input onchange="f_onchange(2)" type="FILE" name="photo" size=40 id="file2"></div> <div class="fileDiv"><input onchange="f_onchange(3)" type="FILE" name="photo" size=40 id="file3"></div> <div class="fileDiv"><input onchange="f_onchange(4)" type="FILE" name="photo" size=40 id="file4"></div> <div class="fileDiv"><input onchange="f_onchange(5)" type="FILE" name="photo" size=40 id="file5"></div> <div class="fileDiv"><input onchange="f_onchange(6)" type="FILE" name="photo" size=40 id="file6"></div> <input type="hidden" name="txtUid" value=<%=uid%>> <div class="fileDiv"> <input type="submit" onclick="return f_check()" name="OK" value="确认上传" > <input type="reset" name="Close" value="取消上传"> </div> </form><%@ page language="java" import="java.util.*" pageEncoding="gbk"%><jsp:directive.page import="com.wad.bean.Photobean"/><jsp:directive.page import="java.text.SimpleDateFormat"/><jsp:directive.page import="com.wad.dao.PhotoDao"/><jsp:directive.page import="com.jspsmart.upload.SmartUpload"/><jsp:directive.page import="com.jspsmart.upload.Files"/><jsp:directive.page import="com.jspsmart.upload.File"/><jsp:directive.page import="java.util.ArrayList"/><jsp:directive.page import="java.util.List"/><% request.setCharacterEncoding("gbk"); //设置编码格式 //得到上传对象 SmartUpload su=new SmartUpload(); //初始化 su.initialize(pageContext); //允许上传的文件类型 su.setAllowedFilesList("jpg,gif"); //上传 su.upload(); //保存上传文件 su.save("/upload"); int uid=Integer.parseInt(su.getRequest().getParameter("txtUid")); //int uid=1; //获取文件路径 获取文件集合的对象 Files fils=su.getFiles(); //创建一个图片的集合 List<Photobean> phoList=new ArrayList<Photobean>(); for(int i=0;i<fils.getCount();i++){ File fil=fils.getFile(i); //循环获取文章 if(fil.getFileName().length()>0){ Photobean photo=new Photobean(); //实例化一个图片对向对象 String path=request.getContextPath()+"/upload/"+fil.getFileName(); //拼凑文件路径 path=new String(path.getBytes("ISO-8859-1"),"gbk"); photo.setP_path(path); //设置图片路径 photo.setP_name(fil.getFileName().substring(0,fil.getFileName().indexOf("."))); photo.setP_state(1); //默认为显示 photo.setP_uid(uid); //用来测试数据 ************************************************ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); photo.setP_outTime(sdf.format(new Date())); //设置图片上传时间 phoList.add(photo); //将图片保存到集合 } } PhotoDao dao=new PhotoDao(); Boolean bool=dao.addPhotoS(phoList); if(bool){ out.print("添加成功"); }else{ out.print("添加失败"); }%> websphere调用https的webservice出错的问题 增加百度搜索结果的富文本 学习的困惑 eclipse中构建路径时怎样能不显示每个jar包的路径 谁能给个 SQL2000 JDBC 最新的驱动啊 这个错误是什么原因导致的呢?以前好用现在就不好用了。 關於Tomcat中連接池配置的疑問,急,在線等 jfreechart画柱状图时如何在添加链接呢?急,在线等 各种大吓,帮帮我啊。 jnative指针指向变量问题 急!替换tomcat在地址栏里的图标 求代码接收网上的邮件,并汇总。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>选择要上传的文件</title>
</head>
<body>
<form action="UploadServlet" method="POST" enctype="multipart/form-data">
<h3>
设置目标文件位置:<input type="text" name="PATH" value="" size="30" /><br>
请选择要上传文件:<input type="file" name="FILE1" value="" width="30" /><br>
<input type="submit" value="上传" />
</h3>
</form>
</body>
</html>
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package com.netbeans.web;import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.*;
import javax.servlet.ServletConfig;/**
*
* @author Administrator
*/
public class UploadServlet extends HttpServlet {
private ServletConfig config=null;
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
final public void init(ServletConfig config)throws ServletException{
this.config=config;
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
int count=0;
SmartUpload myUpload=new SmartUpload();
try {
/* TODO output your page here*/
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet UploadServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h3>");
myUpload.initialize(config, request, response);
myUpload.upload();
String path=myUpload.getRequest().getParameter("PATH");
count=myUpload.save(path);
out.println("已经上传"+count+"个文件<br>");
out.println("已经上传文件路径"+path);
}catch(Exception e){
out.println("错误:"+e.toString());
}
finally {
out.println("</h3></body>");
out.println("</html>");
out.close();
}
} // <editor-fold defaultstate="collapsed" desc="HttpServlet 方法。单击左侧的 + 号以编辑代码。">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
} /**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
} /**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
public String getServletInfo() {
return "Short description";
}// </editor-fold>}jspsmart组件到网上下载.很多...一般小于2M的文件用jspsmart效果比较好
我就要上传图片为例说明一下
<script>
function f_onchange(i){
document.getElementById("img"+i).src=document.getElementById("file"+i).value;
document.getElementById("div"+i).style.display="block";
}
</script>
<form name="form1" enctype="multipart/form-data" method="post" action="<%=request.getContextPath()%>/Action/PhotoAction.jsp">
<div class="fileDiv"><input onchange="f_onchange(1)" type="FILE" name="photo" size=40 id="file1"></div>
<div class="fileDiv"><input onchange="f_onchange(2)" type="FILE" name="photo" size=40 id="file2"></div>
<div class="fileDiv"><input onchange="f_onchange(3)" type="FILE" name="photo" size=40 id="file3"></div>
<div class="fileDiv"><input onchange="f_onchange(4)" type="FILE" name="photo" size=40 id="file4"></div>
<div class="fileDiv"><input onchange="f_onchange(5)" type="FILE" name="photo" size=40 id="file5"></div>
<div class="fileDiv"><input onchange="f_onchange(6)" type="FILE" name="photo" size=40 id="file6"></div>
<input type="hidden" name="txtUid" value=<%=uid%>>
<div class="fileDiv">
<input type="submit" onclick="return f_check()" name="OK" value="确认上传" >
<input type="reset" name="Close" value="取消上传">
</div>
</form>
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<jsp:directive.page import="com.wad.bean.Photobean"/>
<jsp:directive.page import="java.text.SimpleDateFormat"/>
<jsp:directive.page import="com.wad.dao.PhotoDao"/>
<jsp:directive.page import="com.jspsmart.upload.SmartUpload"/>
<jsp:directive.page import="com.jspsmart.upload.Files"/>
<jsp:directive.page import="com.jspsmart.upload.File"/>
<jsp:directive.page import="java.util.ArrayList"/>
<jsp:directive.page import="java.util.List"/>
<% request.setCharacterEncoding("gbk"); //设置编码格式
//得到上传对象
SmartUpload su=new SmartUpload();
//初始化
su.initialize(pageContext);
//允许上传的文件类型
su.setAllowedFilesList("jpg,gif");
//上传
su.upload();
//保存上传文件
su.save("/upload");
int uid=Integer.parseInt(su.getRequest().getParameter("txtUid"));
//int uid=1;
//获取文件路径 获取文件集合的对象
Files fils=su.getFiles();
//创建一个图片的集合
List<Photobean> phoList=new ArrayList<Photobean>();
for(int i=0;i<fils.getCount();i++){
File fil=fils.getFile(i); //循环获取文章
if(fil.getFileName().length()>0){
Photobean photo=new Photobean(); //实例化一个图片对向对象
String path=request.getContextPath()+"/upload/"+fil.getFileName(); //拼凑文件路径
path=new String(path.getBytes("ISO-8859-1"),"gbk");
photo.setP_path(path); //设置图片路径
photo.setP_name(fil.getFileName().substring(0,fil.getFileName().indexOf(".")));
photo.setP_state(1); //默认为显示
photo.setP_uid(uid); //用来测试数据 ************************************************
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
photo.setP_outTime(sdf.format(new Date())); //设置图片上传时间
phoList.add(photo); //将图片保存到集合
}
}
PhotoDao dao=new PhotoDao();
Boolean bool=dao.addPhotoS(phoList);
if(bool){
out.print("添加成功");
}else{
out.print("添加失败");
}
%>