Yee樱花也好
2017-11-12 09:16
采纳率: 100%
浏览 1.7k
已采纳

js 上传图片显示上传失败

希望有大神能帮我看一下以下代码哪里有错误导致上传图片后显示上传失败,拜托了.我基础很差

upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ page import="com.jspsmart.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>推荐书籍上传界面</title>
    <script type="text/javascript">
        function check(){
            var txtName = document.getElementById("nfile").value.length;
            if(txtName==0){
                alert("请先选择一张图片");
                return false;
            } 
        }
    </script>
  </head>

  <body style="background: url(images/changepassword.JPG)">
    <div align="center">
        <form action="uploadservlet" enctype="multipart/form-data" 
        onsubmit="return check()" method="post">
            <h1>推荐书籍上传界面</h1>
            选择书籍封面:<input type="file" name="nfile" id="nfile">
            <br><br>
            请输入推荐书籍名:<input type="text" name="gName">
            <br><br>
            请输入推荐理由:<textarea name="gInfo" cols="20" rows="8">
            </textarea>
            <br><br>
            <input type="submit" value="添加书籍">
        </form>
    </div>
  </body>
</html>


uploadservlet.java

```import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.DBClass;

import com.jspsmart.upload.SmartUpload;

public class uploadservlet extends HttpServlet {

public uploadservlet() {
    super();
}


public void destroy() {
    super.destroy();
}


public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    doPost(request, response);
}



public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("text/html;charset=gbk");
    PrintWriter out = response.getWriter();

    String path;
    //进行smartupload操作
    SmartUpload su = new SmartUpload();
    com.jspsmart.upload.File file = null;
    //初始化smartupload对象
    su.initialize(getServletConfig(), request, response);
    //设置允许上传的文件格式
    String allowed = "jpg,gif,png,bmp";
    //设置不允许上传的文件格式
    String baned = "txt,doc,xls,docx,pdf";
    //设置允许上传文件的大小
    int file_size = 1024*1024*10;
    String exMsg = null;
    try{
        //上传文件的设置
        su.setAllowedFilesList(allowed);
        su.setDeniedFilesList(baned);
        su.setMaxFileSize(file_size);
        //执行上传
        su.setCharset("gbk");
        su.upload();

        String gName = su.getRequest().getParameter("bName");
        String gInfo = su.getRequest().getParameter("bInfo");
        file = su.getFiles().getFile(0);
        String filepath = null;
        if(!file.isMissing()){
            //设置文件存放在服务器的位置
            filepath = "upload\\";
            filepath += file.getFileName();
            //将文件另存为
            file.setCharset("gbk");

            //DBClass db=new DBClass();
            //db.addGoods(gName, gInfo, file.getFileName());
            file.saveAs(filepath,SmartUpload.SAVE_VIRTUAL);
            //System.out.print(filepath);
            out.print("<script type='text/javascript'>");
            out.print("alert('上传成功!');");
            out.print("document.location='index.jsp';");
            out.print("</script>");
        }
    }catch(Exception e){
        e.printStackTrace();
        out.print("<script type='text/javascript'>");
        out.print("alert('上传失败!');");
        out.print("document.location='index.jsp';");
        out.print("</script>");
    }

    out.flush();
    out.close();
}


public void init() throws ServletException {
    // Put your code here
}

}



/**

  • 该类为网上书店操作数据库的公用类
  • 用于数据库连接、查询和更新等操作 */ package bean;

import java.io.*;
import java.util.*;
import java.sql.*;

public class DBClass
{
private String driver;
private String url;
private String username;
private String password;
private Connection connection;
private Statement statement;
private String message="";
PreparedStatement pst = null;

public DBClass(){
    driver="com.mysql.jdbc.Driver";
    url="jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=UTF-8";
    username="root";
    password="123";
    connection=null;
    statement=null;
    message="";
}



/* 连接数据库 */
public void connect(){
    try{
        Class.forName(driver);  
        connection=DriverManager.getConnection(url,username,password);
        statement=connection.createStatement();
    }catch(ClassNotFoundException cnfe){
        message="connection:"+cnfe;
    }catch(SQLException sqle){
        message="executeQuery:"+sqle;
    }
}

/* 执行SQL查询并返回结果 */
public ResultSet executeQuery(String query){
    ResultSet resultset=null;
    try{
        resultset=statement.executeQuery(query);
    }catch(SQLException sqle){
        message="executeQuery:"+sqle;
    }
    return resultset;
}

/* 执行数据库更新操作 */
public void executeUpdate(String command){
    try{

        statement.executeUpdate(command);
    }catch(SQLException sqle){
        message="executeUpdate:"+sqle;
    }
}

public void  addGoods(String gName,String gInfo,String path){
    connect();
    try{
        String sql = "insert into add (gName,gInfo,gPic) " +
                "values (?,?,?)";
        pst = connection.prepareStatement(sql);
        pst.setString(1, gName);
        pst.setString(2, gInfo);
        pst.setString(3, path);
        pst.executeUpdate();
        pst.close();
        connection.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}



/* 关闭数据库连接 */
public void closeConnection(){
    try{
        connection.close();
    }catch(SQLException sqle){
        message="closeConnection:"+sqle;
    }
}

}


  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 海之寒心 2017-11-13 04:45
    已采纳

    用开发者工具和debug联合跟踪一下。

    打赏 评论
  • Yee樱花也好 2017-12-04 02:35

    嘻嘻。谢谢了,已经解决了

    打赏 评论

相关推荐 更多相似问题