<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.jspsmart.upload.*" %>
<%@page import="com.agree.smartupload.IPTimeStamp"%><%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'smartuploaddemo_2.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->  </head>
  
  <body>
   <%request.setCharacterEncoding("UTF-8"); %>
    <%
     SmartUpload smart = new SmartUpload();
     smart.initialize(pageContext);
     smart.upload();
     String name = smart.getRequest().getParameter("uname");
     IPTimeStamp its = new IPTimeStamp(request.getRemoteAddr());
     String ext = smart.getFiles().getFile(0).getFileExt();
     String fileName = its.getIPTimeStampRandom()+"."+ext;
     try{
     smart.getFiles().getFile(0).saveAs(this.getServletContext().getRealPath("/")+"upload"+java.io.File.separator+fileName);
      }catch(Exception e){
      System.out.println("这里错误");
      e.printStackTrace();
      }
     %>
     <h1>姓名:<%=name %></h1><br>
     <img src="../upload/<%=fileName %>">
  </body>
</html>
出现问题:姓名乱码,并且提示文件无法保存!下面的错误代码提示:
com.jspsmart.upload.SmartUploadException: File can't be saved (1120).
at com.jspsmart.upload.File.saveAs(File.java:108)
at com.jspsmart.upload.File.saveAs(File.java:65)
at org.apache.jsp.smartupload.smartuploaddemo_005f3_jsp._jspService(smartuploaddemo_005f3_jsp.java:100)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)

解决方案 »

  1.   

    上传建议采用GBK编码 你换成GBK试试
      

  2.   

    String name = smart.getRequest().getParameter("uname");这句是干吗的,给你个我运行成功的
    uploadimage.jsp
    <%@ page contentType="text/html;charset=gb2312" language="java" import="java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*,java.sql.*,com.jspsmart.upload.*,java.util.*"%>
    <%
    SmartUpload mySmartUpload =new SmartUpload();
    long file_size_max=4000000;
    String fileName2="",ext="",testvar="";
    String url="upload/"; //应保证在根目录中有此目录的存在(也就是说需要自己建立相应的文件夹)
    //初始化
    mySmartUpload.initialize(pageContext);
    //只允许上载此类文件
    try {
    mySmartUpload.setAllowedFilesList("jpg,gif");//此处的文件格式可以根据需要自己修改
    //上载文件
    mySmartUpload.upload();
    } catch (Exception e){
    %>
    <SCRIPT language=javascript>
    alert("只允许上传.jpg和.gif类型图片文件");
    window.location='index.jsp';
    </script>
    <%
    }
    try{
    com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
    if (myFile.isMissing())
    {
    %>
    <SCRIPT language=javascript>
    alert("请先选择要上传的文件");
    window.location='index.jsp';
    </script>
    <%
    }
    else{
    //String myFileName=myFile.getFileName(); //取得上载的文件的文件名
    ext= myFile.getFileExt(); //取得后缀名
    int file_size=myFile.getSize(); //取得文件的大小
    String saveurl="";
    if(file_size<file_size_max){
    //更改文件名,取得当前上传时间的毫秒数值
    Calendar calendar = Calendar.getInstance();
    String filename = String.valueOf(calendar.getTimeInMillis());
    saveurl=application.getRealPath("/")+url;
    saveurl+=filename+"."+ext; //保存路径
    myFile.saveAs(saveurl,SmartUpload.SAVE_PHYSICAL);
    String ret = "alert('上传成功!');";
    ret += "window.location.href='show.jsp';";
    out.print("<script language=javascript>" + ret + "</script>");
    }
    }
    }catch (Exception e){
    out.print(e.toString());
    }
    %>
    index.jsp
    <%@ page contentType="text/html;charset=gb2312" language="java" import="java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*,java.sql.*,com.jspsmart.upload.*,java.util.*"%>
    <html>
    <head>
    <title>请选择上传的图片</title>
    </head>
    <body>
    <form action="uploadimage.jsp" method="post" enctype="multipart/form-data" name="form1">
    <table border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
    <td height="45" align="center" valign="middle">
    <input type="file" name="file">
    </td>
    </tr>
    <tr>
    <td>
    <input type="submit" name="Submit" value="上传">
    </td>
    </tr>
    </table>
    </form>
    </body>
    </html>
     
      

  3.   

    改成GBK编码后,姓名显示不再是乱码了,但是图片上传还是无法保存,报错还是像上面的一样,不知道该如何解决!谢谢你的帮忙啊!!!
      

  4.   


    String name = smart.getRequest().getParameter("uname");这句话是从另一个jsp页面的表单中获取参数“uname”
      

  5.   

    已解决,因为我使用的是getRemoteAddr(),在浏览器中返回的是0:0:0:0:0:0:0:0这样的形式,与我设计的类接收类型不符(类接收0.0.0.0),应该使用getLocalAddr()方法就没有错误了。关于乱码问题:在提交表单的那也的编码设置成gb18030就行了。
      

  6.   

    SmartUpload有点过时,建议使用fileupload.