当前位置: 代码迷 >> JavaScript >> jspSmartUpload 下传组件的应
  详细解决方案

jspSmartUpload 下传组件的应

热度:614   发布时间:2012-11-07 09:56:10.0
jspSmartUpload 上传组件的应

<%@ page contentType="text/html;charset=GBK"%>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<font size=5 color=#FF0000> <b>文件上传----使用jspsmart upload组件</b> </font>
<br>

<form action="uploadfile" method="post" enctype="multipart/form-data">
<p>文件名称: <input type="file" name="file1" size="20" maxlength="80">
</p>
<p>文件名称: <input type="file" name="file2" size="20" maxlength="80">
</p>
<p>文件名称: <input type="file" name="file3" size="20" maxlength="80">
</p>
<p>上传路径: <input type="text" name="path" size="30" maxlength="50"><br>
</p>

<p>附加内容: <input type="text" name="other" size="30" maxlength="50">
</p>
<p><input type="submit" value="上传"> <input type="reset"
value="重置"></p>
</form>

<font size=5 color=#FF0000> <b>文件下载----使用jspsmart upload组件</b> </font>
<br>
<form action="downloadfile" method="post">
<p>下载文件的名称: <input type="text" name="downloadFileName" size="20"
maxlength="80"></p>
<input type="submit" value="下载">
</body>
</form>
</html>


一个Servlet

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Random;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;

import connDB.ConnDB;
import dao.DbDAO;

public class ServletUpload extends HttpServlet {
private ServletConfig config;

final public void init(ServletConfig config) throws ServletException {
? this.config = config;
}

protected void doGet(HttpServletRequest request,
?? HttpServletResponse response) throws ServletException, IOException {
?
? ConnDB conndb = new ConnDB();
? DbDAO dao = new DbDAO();
?
? PrintWriter out = response.getWriter();
? out.println("<HTML>");
? out.println("<BODY BGCOLOR='white'>");
? out.println("<H1>jspSmartUpload : Servlet Sample</H1>");
? out.println("<HR>");
? // 变量定义
? int count = 0;
? SmartUpload mySmartUpload = new SmartUpload();
? try {
?? mySmartUpload.initialize(config, request, response);
?? mySmartUpload.upload();
?? //for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {
?? // com.jspsmart.upload.File myfile = mySmartUpload.getFiles().getFile(i);
??? com.jspsmart.upload.File myfile = mySmartUpload.getFiles().getFile(0);
??? String fileName = myfile.getFileName();

??? String othername = null;
??? String littlename = myfile.getFileExt();
??? Random rd = new Random();
??? Calendar time = Calendar.getInstance();

??? othername = String.valueOf(time.get(Calendar.YEAR))
????? + String.valueOf(time.get(Calendar.MONTH))
????? + String.valueOf(time.get(Calendar.DAY_OF_MONTH))
????? + String.valueOf(time.get(Calendar.HOUR_OF_DAY))
????? + String.valueOf(time.get(Calendar.MINUTE))
????? + String.valueOf(time.get(Calendar.SECOND))+"."+littlename;???
??? myfile.saveAs("/upload/"+ othername,1);
??? //mySmartUpload.SAVE_VIRTUAL;
??? //count = mySmartUpload.save("/upload", 1);
??? // count = mySmartUpload.save(null);
?? //}
???
?? String sql = "update car set url = '"+"upload/"+othername+"'";
?? dao.addImg(sql);
??? out.println("add success! <BR>");
??
??? //out.println("add falure!!!!");
??
?? conndb.close();
?? out.println(count + " file uploaded.");
? } catch (Exception e) {
?? out.println("Unable to upload the file.<br>");
?? out.println("Error : " + e.toString());
? }
? out.println("<jsp:forward path ='success.jsp'>");
? out.println("</BODY>");
? out.println("</HTML>");
}

protected void doPost(HttpServletRequest request,
?? HttpServletResponse response) throws ServletException, IOException {
? // TODO Auto-generated method stub
? doGet(request, response);
}
}

来源:www.sharejava.cn

?