<%
//*******************ÏÈ´¦ÀíÎļþÉÏ´«************
boolean IS_DEBUG = true; //µ÷ÊÔ¿ª¹Ø
int count=0; //¼Ç¼ÎļþµÄ¸öÊý
String[] sSrcFileName = new String[20]; //¼Ç¼ÎļþµÄԭʼ·¾¶
String[] sDstFileName = new String[20]; //¼Ç¼ÎļþÔÚϵͳÖб£´æµÄ·¾¶
mySmartUpload.initialize(pageContext); //³õʼ»¯ÉÏ´«×é¼þ
mySmartUpload.setTotalMaxFileSize(1073741824); //ÉèÖÃÐèÒªÉÏ´«µÄÎļþ´óСΪ10M Éç¿ÆÔºÏîÄ¿
//*********************ÉÏ´«************************
try {
mySmartUpload.upload();
}
catch(Exception ex){
throw new WCMException(WCMException.ERR_WCMEXCEPTION, "ÉÏ´«Îļþʧ°Ü£¡", ex);
}
for (int i=0;i<mySmartUpload.getFiles().getCount();i++){ //´¦ÀíÉÏ´«µÄÎļþ
com.jspsmart.upload.File myFileObj = mySmartUpload.getFiles().getFile(i); //»ñÈ¡Ä¿Ç°Îļþ¶ÔÏó
if(myFileObj.isMissing())continue; //ÅжÏÎļþÊÇ·ñ´æÔÚ
sSrcFileName[count] = myFileObj.getFilePathName(); //¼Ç¼ÎļþµÄԭʼ·¾¶
//caohui@2004-3-6 ´ÓWCM·þÎñÆ÷ÖлñÈ¡ÐèÒªÎļþÃûÒÔ¼°Â·¾¶
String sFileName = myFileObj.getFileName();
sDstFileName[count] = app.getFilesMan().getNextFilePathName(FilesMan.FLAG_UPLOAD, CMyFile.extractFileExt(sFileName));
//¼Ç¼ÎļþÔÚ·þÎñÆ÷µÄ·¾¶
myFileObj.saveAs(sDstFileName[count]); //±£´æÎļþ
count ++; //¼ÇÊýÆ÷¼Ó1
}
//*******************ÏÈ´¦ÀíÎļþÉÏ´«************
boolean IS_DEBUG = true; //µ÷ÊÔ¿ª¹Ø
int count=0; //¼Ç¼ÎļþµÄ¸öÊý
String[] sSrcFileName = new String[20]; //¼Ç¼ÎļþµÄԭʼ·¾¶
String[] sDstFileName = new String[20]; //¼Ç¼ÎļþÔÚϵͳÖб£´æµÄ·¾¶
mySmartUpload.initialize(pageContext); //³õʼ»¯ÉÏ´«×é¼þ
mySmartUpload.setTotalMaxFileSize(1073741824); //ÉèÖÃÐèÒªÉÏ´«µÄÎļþ´óСΪ10M Éç¿ÆÔºÏîÄ¿
//*********************ÉÏ´«************************
try {
mySmartUpload.upload();
}
catch(Exception ex){
throw new WCMException(WCMException.ERR_WCMEXCEPTION, "ÉÏ´«Îļþʧ°Ü£¡", ex);
}
for (int i=0;i<mySmartUpload.getFiles().getCount();i++){ //´¦ÀíÉÏ´«µÄÎļþ
com.jspsmart.upload.File myFileObj = mySmartUpload.getFiles().getFile(i); //»ñÈ¡Ä¿Ç°Îļþ¶ÔÏó
if(myFileObj.isMissing())continue; //ÅжÏÎļþÊÇ·ñ´æÔÚ
sSrcFileName[count] = myFileObj.getFilePathName(); //¼Ç¼ÎļþµÄԭʼ·¾¶
//caohui@2004-3-6 ´ÓWCM·þÎñÆ÷ÖлñÈ¡ÐèÒªÎļþÃûÒÔ¼°Â·¾¶
String sFileName = myFileObj.getFileName();
sDstFileName[count] = app.getFilesMan().getNextFilePathName(FilesMan.FLAG_UPLOAD, CMyFile.extractFileExt(sFileName));
//¼Ç¼ÎļþÔÚ·þÎñÆ÷µÄ·¾¶
myFileObj.saveAs(sDstFileName[count]); //±£´æÎļþ
count ++; //¼ÇÊýÆ÷¼Ó1
}
findme123(飞翔的叶子) 用的就是。我有源码,就是你分太少了
<html>
<META http-equiv=Content-Type content="text/html; charset=gb2312"></META>
<body>
<script language="JavaScript">
<!--
function checkForm(){
if (document.uploadform.uploadfile.value==""){
alert("你必须选择一个文件才能进行上传!");
return false;
}
document.uploadform.submit();
}
function cancelUpload(){
window.close();
window.opener.focus();
}
//-->
</script>
<%@ page import="chapter7.FileUtil" %>
<jsp:useBean id="list" class="chapter7.FileUtil" scope="session" />
<%String path=request.getParameter("path");
if (path==null) { path="d:/"; }%>
<form name="uploadform" method="post"
action="smartupload.jsp" ENCTYPE="multipart/form-data">
<center>
<table border="1" width="65%">
<tr>
<td colspan=2 nowrap align=center>文件上传</td>
</tr>
<tr>
<td width="15%" nowrap >上传文件名称::</td>
<td width="50%" nowrap><input type="file"
name="uploadfile" size="20"></td>
</tr>
<tr>
<td align="center" colspan=2 >
<input type="hidden" name="path" value="<%=list.decodeString(path)%>">
<input type="button" value="上传" onclick="return checkForm();" >
<input type="button" value="取消" onclick="return cancelUpload();">
</td>
</tr>
</form>
</table>
</body> ********* 2:smartupload.jsp ***********
<%@page contentType="text/html; charset=gb2312"%>
<html>
<META http-equiv=Content-Type content="text/html; charset=gb2312"></META>
<body>
<%@ page language="java" import="com.jspsmart.upload.*,
java.sql.*,java.util.*,java.io.* "%>
<%-- 使用SmartUpload Java Bean --%>
<jsp:useBean id="mySmartUpload" scope="page"
class="com.jspsmart.upload.SmartUpload" />
<%
//进行初始化操作
mySmartUpload.init(config);
mySmartUpload.service(request,response); //限制上传文件的类型和大小
mySmartUpload.setAllowedFilesList("bmp,txt,exe");
mySmartUpload.setTotalMaxFileSize(51200); //进行上传操作
try{
mySmartUpload.upload();
}
catch(Exception e){ %>
<font color=red>上传文件失败!</font>
<a href= "JavaScript:window.history.back();">返回</a><br>
<UI>注意事项:</UI>
<UL>你必须设定正确的文件名称!</UL>
<UL>你的文件大小必须小于50K!</UL>
<UL>你只能上传.bmp .txt .exe类型的文件!</UL>
<%return;
}
//将上传文件存盘
String path="./";
com.jspsmart.upload.File upfile=mySmartUpload.getFiles().getFile(0);
upfile.saveAs(path+upfile.getFileName());
%>
<jsp:forward page="list.jsp">
<jsp:param name="path" value="<%=path%>"/>
</jsp:forward>
</body>
</html>
import java.io.*;public class FileUtil{
private String fullPath;
public FileUtil(){
fullPath="c:";
} public FileUtil(String path){
fullPath=path;
} public String getPath(){
return fullPath;
} public String decodeString(String src){
String des=null;
if (src!=null){
des="";
for (int i=0;i<src.length();i++){
if (src.charAt(i)==' '){
des+="%20";
}else{
des+=src.charAt(i);
}
}
}
return des;
} public String addPath(String fileName){
String result="";
if (fullPath.charAt(fullPath.length()-1)=='\\'){
result=fullPath+fileName;
}else{
result=fullPath+'\\'+fileName;
}
return decodeString(result);
} public void setPath(String path){
fullPath=path;
} public String dirUp(String path){
String result="";
File dir=new File(path);
result=dir.getParent();
return decodeString(result);
} public boolean copyFile(String fileName){
return true;
} public boolean pasteFile(String fileName){
return true;
} public boolean cutFile(String fileName){
return true;
} public boolean deleteFile(String path){
try{
File file=new File(path);
return file.delete();
}catch(Exception e){
e.printStackTrace();
return false;
}
} public boolean newDir(String dirName){
try{
File file=new File(fullPath+'\\'+dirName);
return file.mkdirs();
}catch(Exception e){
e.printStackTrace();
return false;
}
} public boolean newFile(String fileName){
try{
File file = new File(fullPath+'\\'+fileName);
return file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
return false;
}
} public static File[] listRoot(){
return File.listRoots();
} public File[] listFiles() {
File dir=new File(fullPath);
String[] ss = dir.list();
if (ss == null)
return null;
int n = ss.length;
File[] fs = new File[n];
for(int i = 0; i < n; i++) {
fs[i] = new File(dir.getPath(), ss[i]);
}
return fs;
}/* public static void main(String[] args){
try{
File[] list=listFiles(new File("c:"));
for (int i=0;i<list.length;i++){
if (list[i].isDirectory()){
System.out.print("<"+list[i].getName()+">");
Date date=new Date(list[i].lastModified());
DateFormat format=DateFormat.getDateInstance();
System.out.println(format.format(date));
}else{
System.out.println(list[i].getName()+"--"+list[i].length());
}
}
System.out.println("hehe");
}catch(Exception e){
e.printStackTrace();
}
}*/
}**************** 4: MyUpload.java ****************
package chapter7;import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyUpload
{
private ServletRequest request;
private ServletResponse response;
private ServletConfig config; ServletInputStream DATA;
int FormSize;
File upfile;
FileOutputStream os;
DataInputStream is;
String filename;
byte[] b;
byte t;
boolean flag=false; public MyUpload (){}public void initialize(ServletConfig config,HttpServletRequest request,HttpServletResponse response) throws IOException{
this.request=request;
this.response=response;
this.config=config;
DATA = request.getInputStream();
FormSize=request.getContentLength();
}
public boolean setFilename(String s){
try{
File f1=new File(s);
os=new FileOutputStream(f1);
}catch(IOException e){
return(false);
}
return true;
}public void getByte(){
int i=0;
try{
is=new DataInputStream(DATA);
b=new byte[FormSize];while (true){
try{
t=is.readByte();
b[i]=t;
i++;
}catch(EOFException e){
break;
}
}
is.close();
}catch(IOException e){}
}public boolean save(){
int i=0,start1=0,start2=0;
String temp="";
if (!flag){
getByte();
flag=true;
}
try{
temp=new String(b,"ISO8859_1");
}
catch(UnsupportedEncodingException e){
return false;
} start1=temp.indexOf("image/");
temp=temp.substring(start1);
start1=temp.indexOf("\r\n\r\n");
temp=temp.substring(start1+4);
start2=temp.indexOf(";\r\n");
if (start2!=-1){
temp=temp.substring(0,start2);
}
try{
byte[] img=temp.getBytes("ISO8859_1");
for (i=0;i<img.length;i++){
os.write(img[i]);
}
os.close();
}
catch(IOException e){
return false;
}
return true;
}
}*********** 5: ************
下载 com.jspsmart 复制到 \WEB-INF\classes\com