lkext 2015-06-14 15:12 采纳率: 50%
浏览 1717

jsp 用 RandomAccessFile上传文件 不知道哪儿有问题

package tom.jiafei;
import java.io.*;
import tom.jiafei.xinxi;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.*;
public class Safe extends HttpServlet {

/**
 * Constructor of the object.
 */
public Safe() {
    super();
}

/**
 * Destruction of the servlet. <br>
 */
public void destroy() {
    super.destroy(); // Just puts "destroy" string in log
    // Put your code here
}

/**
 * The doGet method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to get.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out
            .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
    out.println("<HTML>");
    out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
    out.println("  <BODY>");
    out.print("    This is ");
    out.print(this.getClass());
    out.println(", using the GET method");
    out.println("  </BODY>");
    out.println("</HTML>");
    out.flush();
    out.close();
}

/**
 * The doPost method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to post.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
      File a=new File("D:/ceshi/2/2.txt");
        FileOutputStream a1=new FileOutputStream(a);
        InputStream b1=request.getInputStream();
        byte v[]=new byte[20000];
        int n;
        while((n=b1.read(v))!=-1)
        a1.write(v, 0, n);
        a1.close();
        b1.close();
        String filename="t.txt";
        byte cc[]=filename.getBytes("ISO-8859-1");
        filename=new String(cc);
           File c1=new File("D:/ceshi/2/"+filename);
           RandomAccessFile randomwrite=new RandomAccessFile(c1,"rw");
        RandomAccessFile randomread=new RandomAccessFile(a,"r");
        int second=1;
        String secondLine=null;
        while(second<=2)
        {
        secondLine=randomread.readLine();
        second++;
        }

        randomread.seek(0);
        long four=0;
        int four1=1;
        while((n=randomread.readByte())!=-1&&(four1<=4))
        {
        if(n=='\n')
        {
        four=randomread.getFilePointer();
        four1++;
        }
        }
    randomread.seek(randomread.length());
    long end=randomread.getFilePointer();

    int j=1;
    while( (end>=0) && (j<6))  
        {
              end--;
    randomread.seek(end);
     n=randomread.readByte();

        if(n=='\n')
        {
        end=randomread.getFilePointer();
        j++;
        }
        randomread.seek(four);
        while(four<end-1)
        {
        n=randomread.readByte();
        randomwrite.write(n);
        four=randomread.getFilePointer();
        }
        randomread.close();
        randomwrite.close();



        }





/*  try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (Exception e) {
        // TODO: handle exception
    }
    Connection con;
    Statement sql = null;
    ResultSet rs = null;
    String uri="jdbc:mysql://localhost:3306/qwer?user=root&password=root&characterEncoding=gb2312";
    try {
        con=DriverManager.getConnection(uri);
        sql=con.createStatement();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

xinxi xinxi=new xinxi();
HttpSession session=request.getSession();
session.setAttribute("xinxi",xinxi);
String yonghuming=request.getParameter("yonghuming");
String mima=request.getParameter("mima");
String cha="";
int l=1;
String l1=request.getParameter("jieshou");
l=Integer.parseInt(l1);
String name="";
switch(l)
{
case 10:
{
name="xuesheng";
cha="insert into xuesheng values(3,'xc','xv')";
try {
sql.executeUpdate(cha);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
case 11:
{
name="xuesheng";
cha="update xuesheng set 姓名='123',学号='qwe'";
try {
sql.executeUpdate(cha);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
case 2:
{

    }
    case 3:
    {

    }
    }
    xinxi.setYonghuming(yonghuming);
    xinxi.setMima(mima);


    String ty="select * from jiaoshi";
    try {
    rs= sql.executeQuery(ty);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
RequestDispatcher dispatcher=request.getRequestDispatcher("ceshi.jsp");
dispatcher.forward(request, response);

PrintWriter out=response.getWriter();*/
/*try {
rs.next();

for(int i = 0;i<=rs.getRow();i++)
    out.println(rs.getString(i+1));

    sql.executeUpdate(cha);

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/

}

/**
 * Initialization of the servlet. <br>
 *
 * @throws ServletException if an error occurs
 */
public void init() throws ServletException {
    // Put your code here
}

}

  • 写回答

3条回答 默认 最新

  • lkext 2015-06-14 15:15
    关注

    也就是提取上传文件的第5行到倒数第6行的内容,我都按照书上的标准敲的代码,为什么上传的最后一句总是多一句这个
    ------WebKitFormBoundaryqrFPvEjtqVRR9SD7--
    为什么啊

    评论

报告相同问题?

悬赏问题

  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。