怎么实现文件上传呀?! 看看jspsmartupload 的例子的代码,太简单了只要会用就可以了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 给个使用的代码,以及下载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了 jsp页面后退后刷新 jquery模拟事件 请问,如何使用Proxool配置JNDI数据源,急 【共享】IE下实现3D柱状图(支持链接、事件),javascript+vml实现 JSP中发送邮件的问题 JavaWeb书籍 为什么我的response.Redirect("index.jsp");转向不了???怪 谁有jfreechart和jfreereport的资料啊? 关于文件下载问题求救!!!!!!!!!!!!!! 使用jave的jar包处理音频在Linux报错 如何判断表单中的内容为空?? 这样合不合理????? 我看过一个例子,jsp + servlet实现
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
你要加上数据库操作,最好还是用自己的XXX.jsp文件。但调用的是jspsmartUpload的bean
请高手帮忙!谢谢!
java.lang.IllegalArgumentException: This path does not exist (1135).
数据库中表格建立的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!