日期:2014-05-17  浏览次数:20689 次

求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