to fastmask(我心烦) 能给例子?

解决方案 »

  1.   

    一般 欧是用 jspsmartupload 的
    所以具体过程不太了解不好意思
      

  2.   

    jspsmartupload的开销更大。没有人知道了吗?
      

  3.   

    你的意思是自己写上传bean,把上传的文件用流的方式上传吧,但这样有些复杂,这是我朋友的一个例子,你看看,比较长,而且他有写数据库的部分,我把它们删除了,但原理正确。import java.io.*;
    import java.lang.*;
    import java.util.*;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.ServletInputStream;
    import javax.servlet.ServletException;
    public class upload{private static String newline = "\n";
    private String RelativeDir = "";
    private String uploadDirectory = ".";
    private String ContentType = "";
    private String CharacterEncoding = "";private int    var_num=9;
    private String parameterNames[] = new String[var_num+1];public upload()
    {}private String getFileName(String s){ int i = s.lastIndexOf("\\"); if(i < 0 ){ // i >= s.length() - 1
    i = s.lastIndexOf("/");
    if(i < 0 ) // i >= s.length() - 1
    return s;
    }
    return s.substring(i + 1);
    }public void setRelativeDir(String s){
    RelativeDir=s;
    }public void setUploadDirectory(String s){
    uploadDirectory = s;
    }public void setParameterNames(String s[]){ for(int i=0; i<var_num; i++){
    parameterNames[i] = s[i];
    }
    }public void setContentType(String s){
    ContentType = s;
    int j;
    if((j = ContentType.indexOf("boundary=")) != -1){
    ContentType = ContentType.substring(j + 9);
    ContentType = "--" + ContentType;
    }
    }public void setCharacterEncoding(String s){
    CharacterEncoding = s;
    }public String uploadFile( HttpServletRequest req) throws ServletException, IOException{
    setCharacterEncoding(req.getCharacterEncoding());
    setContentType(req.getContentType()); return(uploadFile(req.getInputStream()));
    }public String uploadFile( ServletInputStream servletinputstream) throws ServletException, IOException{ String s5 = null;
    String filename = null;
    byte Linebyte[] = new byte[4096];
    byte outLinebyte[] = new byte[4096];
    int ai[] = new int[1];
    int ai1[] = new int[1]; String line; //先增加的变量
    String tmp_type = "";  //文件类型
    String tmp_name = "";  //文件名
    long   tmp_size = 0;   //文件大小 String tmp_array[]=new String[var_num+3];
    String var_str = "";
    String return_str = "";
    int    l=0; //得到request传递的各变量的值
    while((line = readLine(Linebyte, ai, servletinputstream, CharacterEncoding)) != null){ if(parameterNames[l]!=null){
    var_str = "name=\"" + parameterNames[l] + "\"";
    int tmp_pos=line.indexOf(var_str);
    if(tmp_pos >= 0){
    line = readLine(Linebyte, ai, servletinputstream, CharacterEncoding);
    line = readLine(Linebyte, ai, servletinputstream, CharacterEncoding); tmp_array[3+l] = line.trim();
    l++;
    }
    }
    else {
    int i = line.indexOf("filename=");
    if(i >= 0){
    line = line.substring(i + 10); if((i = line.indexOf("\"")) > 0)
    line = line.substring(0, i);
    break;
    }
    }
    } //获取文件信息并上传
    filename = line; if(filename != null && !filename.equals("\"")){ filename = getFileName(filename); int j=filename.indexOf(".");
    if(j>0){
    tmp_type=filename.substring(j+1);
    tmp_name=filename.substring(0,j);
    } String sContentType = readLine(Linebyte, ai, servletinputstream, CharacterEncoding); if(sContentType.indexOf("Content-Type") >= 0)
    readLine(Linebyte, ai, servletinputstream, CharacterEncoding); //File(String parent, String child)
    //Creates a new File instance from a parent pathname string
    //and a child pathname string.
    File file = new File(uploadDirectory, filename); int k=0;
    String old_name=tmp_name; while(file.exists()){
    k++;
    tmp_name=old_name + k;
    filename=tmp_name + "." + tmp_type;
    file = new File(uploadDirectory, filename);
    }
    tmp_array[0]=tmp_name;
    tmp_array[1]=tmp_type;
    tmp_array[2]=""; //文件大小fileoutputstream = new FileOutputStream(file); while((sContentType = readLine(Linebyte, ai, servletinputstream, CharacterEncoding)) != null){
    if(sContentType.indexOf(ContentType) == 0 && Linebyte[0] == 45)
    break; if(s5 != null){
    //write(byte[] b, int off, int len)
    //Writes len bytes from the specified byte array starting
    //at offset off to this file output stream.
    fileoutputstream.write(outLinebyte, 0, ai1[0]);
    fileoutputstream.flush();
    }
    s5 = readLine(outLinebyte, ai1, servletinputstream, CharacterEncoding);
    if(s5 == null ) // s5.indexOf(ContentType) == 0 && outLinebyte[0] == 45
    break; fileoutputstream.write(Linebyte, 0, ai[0]);
    fileoutputstream.flush();
    } byte byte0; if(newline.length() == 1)
    byte0 = 2;
    else
    byte0 = 1; if(s5 != null && outLinebyte[0] != 45 && ai1[0] > newline.length() * byte0)
    fileoutputstream.write(outLinebyte, 0, ai1[0] - newline.length() * byte0); if(sContentType != null && Linebyte[0] != 45 && ai[0] > newline.length() * byte0)
    fileoutputstream.write(Linebyte, 0, ai[0] - newline.length() * byte0); fileoutputstream.close();
    return (return_str);
    }//获取非文件的变量值
    public String getParameter(HttpServletRequest req, String s) throws ServletException, IOException{ return (getParameter(req.getInputStream(),s));}public String getParameter( ServletInputStream servletinputstream,String s) throws ServletException, IOException{ byte Linebyte[] = new byte[4096];
    int ai[] = new int[1]; String line="";
    String tmp_str=""; //得到变量值
    int j=1;
    tmp_str = "name=\"" + s + "\"";
    while((line = readLine(Linebyte, ai, servletinputstream, CharacterEncoding))!=null){ tmp_str = "name=\"" + s + "\"";
    int i = line.indexOf(tmp_str);
    if(i >= 0){
    line = readLine(Linebyte, ai, servletinputstream, CharacterEncoding);
    line = readLine(Linebyte, ai, servletinputstream, CharacterEncoding);
    break;
    } }
    return tmp_str;
    }private String readLine(byte Linebyte[], int ai[], ServletInputStream servletinputstream, String CharacterEncoding){
    try{
    //readLine(byte[] buffer, int offset, int length)
    //Reads a line from the POST data.
    ai[0] = servletinputstream.readLine(Linebyte, 0, Linebyte.length);
    if(ai[0] == -1)
    return null;
    }catch(IOException _ex){
    return null;
    }
    try{
    if(CharacterEncoding == null){
    //用缺省的编码方式把给定的byte数组转换为字符串
    //String(byte[] bytes, int offset, int length)
    return new String(Linebyte, 0, ai[0]);
    }else{
    //用给定的编码方式把给定的byte数组转换为字符串
    //String(byte[] bytes, int offset, int length, String enc)
    return new String(Linebyte, 0, ai[0], CharacterEncoding);
    }
    }catch(Exception _ex){
    return null;
    }
    }
    }
      

  4.   

    http://expert.csdn.net/Expert/topic/1192/1192621.xml?temp=.6103632
      

  5.   

    to  zxhong(红)那个程序明显开了整个上传请求的buffer进行处理。开销太大了。
      

  6.   

    to xinlcao
    你的程序测试半通过……有的文件上传后发现文件结尾多了2字节(一个回行)知道原因吗?
      

  7.   

    to xinlcao这是我最后运行的代码,你可以试一下,举例说,如果你上传一个0字节的空文件试试?(我这里传上去就变成2字节了)谢谢!import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;public class UploadToFS extends HttpServlet { private static String newline = "\n";
    private String RelativeDir = "";
    private String uploadDirectory = "D:\\";
    private String ContentType = "";
    private String CharacterEncoding = ""; private int var_num = 9;
    private String parameterNames[] = new String[var_num + 1]; private String getFileName(String s) { int i = s.lastIndexOf("\\"); if (i < 0) { // i >= s.length() - 1
    i = s.lastIndexOf("/");
    if (i < 0) // i >= s.length() - 1
    return s;
    }
    return s.substring(i + 1);
    } public void setRelativeDir(String s) {
    RelativeDir = s;
    } public void setUploadDirectory(String s) {
    uploadDirectory = s;
    } public void setParameterNames(String s[]) { for (int i = 0; i < var_num; i++) {
    parameterNames[i] = s[i];
    }
    } public void setContentType(String s) {
    ContentType = s;
    int j;
    if ((j = ContentType.indexOf("boundary=")) != -1) {
    ContentType = ContentType.substring(j + 9);
    ContentType = "--" + ContentType;
    }
    } public void setCharacterEncoding(String s) {
    CharacterEncoding = s;
    } public String uploadFile(HttpServletRequest req) throws ServletException, IOException {
    setCharacterEncoding(req.getCharacterEncoding());
    setContentType(req.getContentType()); return (uploadFile(req.getInputStream()));
    } public String uploadFile(ServletInputStream servletinputstream)
    throws ServletException, IOException { String s5 = null;
    String filename = null;
    byte Linebyte[] = new byte[4096];
    byte outLinebyte[] = new byte[4096];
    int ai[] = new int[1];
    int ai1[] = new int[1]; String line; //先增加的变量
    String tmp_type = ""; //文件类型
    String tmp_name = ""; //文件名
    long tmp_size = 0; //文件大小 String tmp_array[] = new String[var_num + 3];
    String var_str = "";
    String return_str = "";
    int l = 0; //得到request传递的各变量的值
    while ((line = readLine(Linebyte, ai, servletinputstream, CharacterEncoding)) != null) { if (parameterNames[l] != null) {
    var_str = "name=\"" + parameterNames[l] + "\"";
    int tmp_pos = line.indexOf(var_str);
    if (tmp_pos >= 0) {
    line = readLine(Linebyte, ai, servletinputstream, CharacterEncoding);
    line = readLine(Linebyte, ai, servletinputstream, CharacterEncoding); tmp_array[3 + l] = line.trim();
    l++;
    }
    } else {
    int i = line.indexOf("filename=");
    if (i >= 0) {
    line = line.substring(i + 10); if ((i = line.indexOf("\"")) > 0)
    line = line.substring(0, i);
    break;
    }
    }
    } //获取文件信息并上传
    filename = line; if (filename != null && !filename.equals("\"")) { filename = getFileName(filename); int j = filename.indexOf(".");
    if (j > 0) {
    tmp_type = filename.substring(j + 1);
    tmp_name = filename.substring(0, j);
    } String sContentType = readLine(Linebyte, ai, servletinputstream, CharacterEncoding); if (sContentType.indexOf("Content-Type") >= 0)
    readLine(Linebyte, ai, servletinputstream, CharacterEncoding); //File(String parent, String child)
    //Creates a new File instance from a parent pathname string
    //and a child pathname string.
    File file = new File(uploadDirectory, filename); int k = 0;
    String old_name = tmp_name; while (file.exists()) {
    k++;
    tmp_name = old_name + k;
    filename = tmp_name + "." + tmp_type;
    file = new File(uploadDirectory, filename);
    }
    tmp_array[0] = tmp_name;
    tmp_array[1] = tmp_type;
    tmp_array[2] = ""; //文件大小 FileOutputStream fileoutputstream = new FileOutputStream(file); while ((sContentType = readLine(Linebyte, ai, servletinputstream, CharacterEncoding))
    != null) {
    if (sContentType.indexOf(ContentType) == 0 && Linebyte[0] == 45)
    break; if (s5 != null) {
    //write(byte[] b, int off, int len)
    //Writes len bytes from the specified byte array starting
    //at offset off to this file output stream.
    fileoutputstream.write(outLinebyte, 0, ai1[0]);
    fileoutputstream.flush();
    }
    s5 = readLine(outLinebyte, ai1, servletinputstream, CharacterEncoding);
    if (s5 == null)
    // s5.indexOf(ContentType) == 0 && outLinebyte[0] == 45
    break; fileoutputstream.write(Linebyte, 0, ai[0]);
    fileoutputstream.flush();
    } byte byte0; if (newline.length() == 1)
    byte0 = 2;
    else
    byte0 = 1; if (s5 != null && outLinebyte[0] != 45 && ai1[0] > newline.length() * byte0)
    fileoutputstream.write(outLinebyte, 0, ai1[0] - newline.length() * byte0); if (sContentType != null && Linebyte[0] != 45 && ai[0] > newline.length() * byte0)
    fileoutputstream.write(Linebyte, 0, ai[0] - newline.length() * byte0); fileoutputstream.close();
    return (return_str);
    } else return "";
    } public String getParameter(HttpServletRequest req, String s)
    throws ServletException, IOException {
    return (getParameter(req.getInputStream(), s));
    } public String getParameter(ServletInputStream servletinputstream, String s)
    throws ServletException, IOException { byte Linebyte[] = new byte[4096];
    int ai[] = new int[1]; String line = "";
    String tmp_str = ""; //得到变量值
    int j = 1;
    tmp_str = "name=\"" + s + "\"";
    while ((line = readLine(Linebyte, ai, servletinputstream, CharacterEncoding)) != null) { tmp_str = "name=\"" + s + "\"";
    int i = line.indexOf(tmp_str);
    if (i >= 0) {
    line = readLine(Linebyte, ai, servletinputstream, CharacterEncoding);
    line = readLine(Linebyte, ai, servletinputstream, CharacterEncoding);
    break;
    } }
    return tmp_str;
    } private String readLine(
    byte Linebyte[],
    int ai[],
    ServletInputStream servletinputstream,
    String CharacterEncoding) {
    try {
    //readLine(byte[] buffer, int offset, int length)
    //Reads a line from the POST data.
    ai[0] = servletinputstream.readLine(Linebyte, 0, Linebyte.length);
    if (ai[0] == -1)
    return null;
    } catch (IOException _ex) {
    return null;
    }
    try {
    if (CharacterEncoding == null) {
    //用缺省的编码方式把给定的byte数组转换为字符串
    //String(byte[] bytes, int offset, int length)
    return new String(Linebyte, 0, ai[0]);
    } else {
    //用给定的编码方式把给定的byte数组转换为字符串
    //String(byte[] bytes, int offset, int length, String enc)
    return new String(Linebyte, 0, ai[0], CharacterEncoding);
    }
    } catch (Exception _ex) {
    return null;
    }
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
    uploadFile(req);
    System.out.println("OK!");
    }}