2 u013031331 u013031331 于 2015.06.14 23:12 提问

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

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个回答

u013031331
u013031331   2015.06.14 23:15

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

bulusli3
bulusli3   Ds   Rxr 2015.06.16 11:38

 “------webkitformboundary” 是safari 浏览器从客户端向服务器端传送HTML标签数据所使用的分隔符(一般会在分隔符后附加一串十六进制的数以示区别)。这里所指的HTML标签数据包括文本框、密码框、文件上传框......一般来说,这些网页元素都被嵌入于form表单,所以才称为:formboundary。

  客户端需要把网页数据打包成一个数据包发送给服务器,而组装的时候便是用分隔符把所有网页元素的标签数据分隔起来。服务器收到数据包后,先从数据包的头部固定位置寻找到分隔符字串,再据此拆分整个数据包并从中一一读取HTML的所有标签数据。

u013031331
u013031331 问题是怎么解决,我新发布了一个,精简了,希望帮忙看一下。
2 年多之前 回复
twfx1027
twfx1027   2015.06.16 15:02

这么多代码啊,能不能简化下 在提问呢

u013031331
u013031331 不好意思啊,我修改了一下
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片