ackage csjava;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.jspsmart.upload.*;
public class servletUpload extends HttpServlet{
private ServletConfig config; final public void init(ServletConfig config) throws ServletException{
this.config=config;
}
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
PrintWriter out=response.getWriter();
out.println("<HTML>");
out.println("<BODY BGCOLOR='white'>");
out.println("<H1>jspSmartUpload:Servlet Sample</H1>");
out.println("<HR><BR>");
out.println("The method of the HTML form must be POST.");
out.println("</BODY>");
out.println("</HTML>");
}
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
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();
count = mySmartUpload.save(mySmartUpload.getRequest().getParameter("PATH"));
out.println(count+"file uploaded.");
}catch(Exception e){
out.println("Unable to upload the file.<br>");
out.println("Error:"+e.toString());
}
out.println("</BODY>");
out.println("</HTML>");

}

}

解决方案 »

  1.   


    <%@ page contentType="text/html;charset=GB2312" %>
    <%@ page language="java" import="com.jspsmart.upload.*"%>
    <jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" /><HTML>
    <BODY BGCOLOR="white"><H1>jspSmartUpload : Sample 2</H1>
    <HR><%
    // 声明变量count,用来存储上传文件个数
    int count=0; // 执行初始化操作
    mySmartUpload.initialize(pageContext); // 上传文件到服务器
    mySmartUpload.upload(); // 对上传到服务器的文件进行逐个处理
    for (int i=0;i<mySmartUpload.getFiles().getCount();i++){ // 取出一个文件
    com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i); // 只有myFile代表的文件存在时才执行存储操作
    if (!myFile.isMissing()) { // 保存该文件到web应用程序下的upload目录
    myFile.saveAs("/upload/" + myFile.getFileName());
    //  显示当前文件信息
    out.println("FieldName = " + myFile.getFieldName() + "<BR>");
    out.println("Size = " + myFile.getSize() + "<BR>");
    out.println("FileName = " + myFile.getFileName() + "<BR>");
    out.println("FileExt = " + myFile.getFileExt() + "<BR>");
    out.println("FilePathName = " + myFile.getFilePathName() + "<BR>");
    out.println("ContentType = " + myFile.getContentType() + "<BR>");
    out.println("ContentDisp = " + myFile.getContentDisp() + "<BR>");
    out.println("TypeMIME = " + myFile.getTypeMIME() + "<BR>");
    out.println("SubTypeMIME = " + myFile.getSubTypeMIME() + "<BR>"); count ++; } } // Display the number of files which could be uploaded
    out.println("<BR>" + mySmartUpload.getFiles().getCount() + " files could be uploaded.<BR>"); // Display the number of files uploaded
    out.println(count + " file(s) uploaded.");
    %>
    </BODY>
    </HTML>
      

  2.   

    执行后报错
    type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: An exception occurred processing JSP page /uploadfile.jsp at line 2724:     mySmartUpload.initialize(pageContext);
    25: 
    26:     // 上传文件到服务器
    27:     mySmartUpload.upload();
    28: 
    29:     // 对上传到服务器的文件进行逐个处理
    30:     for (int i=0;i<mySmartUpload.getFiles().getCount();i++){
      

  3.   

    public boolean uploadTempFile(File file, String fileName,
    String tempFilePath) {
    InputStream inputStream = null;
    OutputStream outputStream = null;
    try {
    File tempFileDirectory = new File(tempFilePath);
    if (!tempFileDirectory.isDirectory()) {
    if (!tempFileDirectory.mkdirs()) {
    return false;
    }
    } File tempFile = new File(tempFileDirectory, fileName);
    if (tempFile.exists()) {
    tempFile.delete();
    }
    inputStream = new FileInputStream(file);
    outputStream = new FileOutputStream(tempFile);
    int byteRead = 0;
    byte[] buffer = new byte[BUFFER_SIZE];
    while ((byteRead = inputStream.read(buffer)) != -1) {
    outputStream.write(buffer, 0, byteRead);
    }
    outputStream.close();
    inputStream.close();
    return true;
    } catch (Exception ex) {
    return false;
    }
    }