怎么实现文件上传呀

 首页 本类怎么实现文件上传呀?!
我需要把文件通过页面上传到服务器的特定文件夹下,同时把上传文件的文件名存入数据库,我已经实现了后一项,可是到底怎么上传文件呢?我以前使用php实现上传好象挺容易的,可是jsp要怎么写呀?我一点头绪都没有,去看过其他的帖子了,可是好象没有一个说了清楚的,还望哪位高手予以指点!感激之至!

看看jspsmartupload 的例子的代码,太简单了只要会用就可以了。

给个使用的代码,以及下载jspsmartload的地址。感激不尽!

什么都能上传
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.neusoft.ExtendLibraries.ExtendConfig.configInfo;
public class ImportServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
private static final int ONE_MEG = 2048*1024;
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
int buffsize = 1024*8;
int contentLength = request.getContentLength();
// Hashtable pageItems = null;
// value chosen to limit denial of service - 2 MB, in this case
if (contentLength > ONE_MEG){
System.out.println ("contentLength > ONE_MEG");
response.setContentType("text/html");
ServletOutputStream out = response.getOutputStream();
out.println("<html><head><title>Too big</title></head>");
out.println("<body><h1>错误 - 文件大于2M!");
out.println("</h1></body></html>");
out.close();
}
else{
HttpSession session = request.getSession(true);
int status=0;//status=0:非正文;status=1:分隔符;status=2:文件内容;status=3:其它内容
String ContentType=request.getHeader("content-type");
String boundary=ContentType.substring(ContentType.indexOf("boundary=")+9);
String fileName="";
String itemName="";
String lastItem="";
Properties pp = new Properties();
try{
pp = configInfo.getConfigObj();
}catch(Exception e){
}
String strPath=pp.getProperty("OutputPath");// "D://Application_Studio//JTP//lib//";
ServletInputStream in = request.getInputStream();
byte[] b = new byte[buffsize];
byte[] byContent = new byte[contentLength];
int result;
int totalRead = 0;
FileOutputStream fileout = null;
try{
result = in.readLine(b, 0, b.length);
while (result != -1){
String rowStr=new String(b,0,result);
if (IsBoundary(rowStr,boundary)){ //遇到分割符
switch(status){
case 0:break;
case 1:break;
case 2:
if ( totalRead!=0&!fileName.equals("")){
fileout =new FileOutputStream(strPath+fileName);
fileout.write(byContent, 0, totalRead);
totalRead=0 ;
}
break;
case 3:
String itemContent=new String(byContent,0,totalRead);
if (itemName.equals(lastItem)){
String sval =(String) session.getAttribute (itemName);
sval+=";"+ itemContent;
session.setAttribute(itemName, sval);
}
else{
session.setAttribute(itemName, itemContent);
}
lastItem=itemName;
break;
}
totalRead=0;
status=0;
}
else if ( status<2 ){ //非正文内容
if (IsFile(rowStr)){
fileName=getFilename(rowStr);
if (!fileName.equals("")){
session.setAttribute("filename", "<A href=http://192.168.66.87:8080/upload/"+ fileName+">"+ fileName+"</A>");
}
result = in.readLine(b, 0, buffsize);//跳过冗余信息
result = in.readLine(b, 0, buffsize);//跳过冗余信息
status=2;
}
else if (IsElement(rowStr)){
itemName=getElename(rowStr);
status=3;
}
}
else { //上载文件内容或表单提交的数据
for ( int i=0; i<result; i++ ){
byContent[totalRead+i]=b[i];
}
totalRead+=result;
}
result = in.readLine(b, 0, buffsize);
}
fileout.close();
}catch (IOException ioe){
System.out.println("IOException at Import File:"+ioe.getMessage());
ioe.printStackTrace();
}
}//end if (contentLength > ONE_MEG)
response.setContentType("text/html");
PrintWriter out = new PrintWriter (response.getOutputStream());
out.println("<html>");
out.println("<head><title>upload</title></head>");
out.println("<body>");
out.println("文件上传成功!");
out.println("</body></html>");
out.close();
}//end doPost()
public String getServletInfo(){
return "A servlet that shows the request headers and body sent by the client";
}
public boolean IsBoundary(String str,String boundary){
if(str.length() < 40 & str.length() > 60 ) return false;
if(str.indexOf(boundary)!=-1) return true;
else return false;
}
public boolean IsFile(String str){
if(str.length() < 53 & str.length() >253) return false;
if(str.indexOf("filename=")!=-1) return true;
else return false;
}
public boolean IsElement(String str){
if(str.length() < 40 & str.length() >253) return false;
if(str.indexOf("name=")!=-1) return true;
else return false;
}
public String getFilename(String str){
String pathName=str.substring(str.indexOf("filename="));
int startIndex=pathName.lastIndexOf("//")+1;
int endIndex=pathName.length()-3;
return pathName.substring(startIndex,endIndex);
}
public String getElename(String str){
String tempStr=str.substring(str.indexOf("name=/"")+6);
return tempStr.substring(0,tempStr.indexOf("/""));
}
//Clean up resources
public void destroy() {
}
}
www.jspsmart.com

您给的是一个javaBean吧?可是怎么用呀?您能再给一个简单的调用的过程的代码吗?另外,调用这个javaBean时,是否还可以同时进行其他的操作呀?因为我还要将文件名存入数据库呀!请帮忙了,谢谢!

如果你没什么特别的要求,就用jspsmartUpload的例子中的东东吧,
你要加上数据库操作,最好还是用自己的XXX.jsp文件。但调用的是jspsmartUpload的bean

我调用jspsmartUpload的bean时出现底下错误信息?
请高手帮忙!谢谢!
java.lang.IllegalArgumentException: This path does not exist (1135).

你看一下jspsmartUpload中的Sample4.htm和Sample4.jsp两个页面的代码,
数据库中表格建立的sql语句:
CREATE TABLE TFILES (ID INT(4) NOT NULL, FILENAME VARCHAR(255), FILE LONGBLOB, PRIMARY KEY (ID));
INSERT TABLE TFILES (ID, FILENAME, FILE) VALUES (1,"sample",null);
good luck!

使用jspsmartupload组件吧,到www.jspsmart下载一个,自己看看源代码就ok了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值