当前位置: 代码迷 >> Java Web开发 >> 求java下传图片、文件(音乐,压缩文件,文本之类的)代码
  详细解决方案

求java下传图片、文件(音乐,压缩文件,文本之类的)代码

热度:8553   发布时间:2013-02-25 21:13:09.0
求java上传图片、文件(音乐,压缩文件,文本之类的)代码
求java上传图片、文件(音乐,压缩文件,文本之类的)代码
额是新手,请前辈指教~
希望得到详细代码,能给整个例子额感激不尽~

------解决方案--------------------------------------------------------
贴段代码给你。要给分哦!!
servlet
Java code
import java.io.BufferedInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.util.Calendar;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class Upload extends HttpServlet {    @Override    protected void doGet(HttpServletRequest request,            HttpServletResponse response) throws ServletException, IOException {        doPost(request, response);    }    @Override    protected void doPost(HttpServletRequest request,            HttpServletResponse response) throws ServletException, IOException {        request.setCharacterEncoding("UTF-8");        javax.servlet.ServletInputStream inputStream = request.getInputStream();        int position=-1;        FileOutputStream outputStream = null;        byte[] buffer = new byte[1024];            position=inputStream.readLine(buffer, 0, buffer.length);        String breakStr = "";        String objRef = "";        if(position!=-1){            breakStr = new String(buffer,0,28);            objRef = new String(buffer,29,position);            System.out.println(breakStr);            System.out.println(objRef);        }        position = inputStream.readLine(buffer, 0, buffer.length);        String upFileName = "";        String fileType = "";        if(position!=-1){            upFileName = new String(buffer,0,position);            System.out.println(upFileName);            int fileNamePosition = 0;            if((fileNamePosition=upFileName.indexOf("filename=\""))!=-1){                fileType=upFileName.substring(fileNamePosition).split("\"")[1];                    outputStream=new FileOutputStream("d://Upload"                        + Calendar.getInstance().getTimeInMillis()+ fileType);            }        }        position = inputStream.readLine(buffer, 0, buffer.length);        String type="";        if(position!=-1){            type = new String(buffer,0,position);            System.out.println(type);        }        String isBreakStr="";        inputStream.readLine(buffer, 0, buffer.length);        while((position=inputStream.readLine(buffer, 0, buffer.length))!=-1){            isBreakStr = new String(buffer, 0, position);            if (isBreakStr.length() > 28                    && isBreakStr.substring(0, 28).equals(breakStr)) {                break;            }            outputStream.write(buffer, 0, position);        }            outputStream.flush();        inputStream.close();        outputStream.close();    }}
------解决方案--------------------------------------------------------
有一个upload有关的java类包,里面有页面代码和几个java类。还有一个上传图片的例子。跟上传文件差不多。楼主要不要。

------解决方案--------------------------------------------------------
java上传功能源码http://download.csdn.net/source/970570

javabean实现上传功能源码http://download.csdn.net/source/970479


仿163网盘无刷新多附件上传源码http://download.csdn.net/source/1037046
------解决方案--------------------------------------------------------
SmartUpload实现上传:
Java code
smartupload上传文件中应该注意的地方是表单中<form enctype="multipart/form-data" name="form1" action="zhbupload" method="post" >的enctype="multipart/form-data"是必须的,还有用了jspSmartupload以后jsp中的request就失效了,要用Smartupload中的Request myrequest = null;mySmartUpload.upload();myrequest = mySmartUpload.getRequest();通过myrequest来获取表单中除了上传文件的表单的其他内容!!还有数据库里面file的数据类型一定要是LongBlob!!package Action;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.PrintWriter;import java.sql.PreparedStatement;import java.util.Calendar;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 Other.DataBaseConnection;import com.jspsmart.upload.Request;import com.jspsmart.upload.SmartUpload;public class Picupload extends HttpServlet {    private static final long serialVersionUID = 1L;    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        response.setContentType("text/html;charset=gb2312");        PrintWriter out = response.getWriter();        SmartUpload mySmartUpload = new SmartUpload();        Request myrequest = null;        String username = "", pwd = "";        // long file_size_max = 4000000;        String ext = "";        String url = "touxiang\\"; // 应保证在根目录中有此目录的存在        // 初始化        mySmartUpload.initialize(this.getServletConfig(), request, response);        // 只允许上载此类文件        try {            mySmartUpload.setAllowedFilesList("jpg,gif");            // 上载文件            mySmartUpload.upload();            myrequest = mySmartUpload.getRequest();        } catch (Exception e) {            out.print("<script>");            out                    .println("alert('只允许上传.jpg和.gif类型图片文件!');window.location='user_register.jsp';");            out.print("</script>");        }        try {            com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(                    0);            int count = 0;            if (myFile.isMissing()) {                out.print("<script>");                out.println("alert('请至少选择一个要上传的文件!');window.location='user_register.jsp';");                out.print("</script>");            } else {                // String myFileName=myFile.getFileName(); //取得上载的文件的文件名                out.println("正在上传文件,请等待。。。。。");                HttpSession session = request.getSession();                for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {                    myFile = mySmartUpload.getFiles().getFile(i);                    if (myFile.isMissing())                        continue;                    ext = myFile.getFileExt(); // 取得后缀名                    int file_size = myFile.getSize(); // 取得文件的大小                    String saveurl = "";                    // 更改文件名,取得当前上传时间的毫秒数值                    Calendar calendar = Calendar.getInstance();                    String filename = String                            .valueOf(calendar.getTimeInMillis());                    saveurl = request.getRealPath("\\") + url;                    saveurl += filename + "." + ext; // 最终文件的保存路径                    System.out.println(saveurl);                    myFile.saveAs(saveurl, mySmartUpload.SAVE_PHYSICAL);                    username = myrequest.getParameter("username");                    pwd = myrequest.getParameter("pwd");                    PreparedStatement pstmt = null;                    DataBaseConnection dbc = null;                    dbc = new DataBaseConnection();                    FileInputStream fis;                    File file;                    file = new File(saveurl);                    fis = new FileInputStream(file);                                        pstmt = dbc                            .getConnection()                            .prepareStatement(                                    "insert into user(username,pwd,pic,userid) values(?,?,?,?)");                    pstmt.setString(1, username);                    pstmt.setString(2, pwd);                    pstmt.setBinaryStream(3, fis, (int) file.length());                    pstmt.setInt(4, 1);                    System.out.println("success inserted user into database");                    pstmt.execute();                    pstmt.close();                    dbc.close();                }                out.print("<script>");                out.print("alert('上传成功');");                out.print("window.location='login.jsp'");                out.print("</script>");            }        } catch (Exception e) {            e.toString();        }    }}
  相关解决方案