try {
            URL url=new URL("http://192.168.0.9:8080/new/wo.mp3");
            HttpURLConnection UrlCon=(HttpURLConnection) url.openConnection();
            UrlCon.setConnectTimeout(60000);
            UrlCon.setRequestMethod("GET");
            System.out.println(UrlCon.getResponseCode());
            if(UrlCon.getResponseCode()==200){
                OutputStream output=UrlCon.getOutputStream();
                File file = new File("f://wo.mp3");
                FileInputStream input = new FileInputStream(file);   
                byte[] buffer=new byte[4*1024];
                int len;
                long start=System.currentTimeMillis();
                while((len=input.read(buffer))!=-1){
                    output.write(buffer,0,len);
                }
               
                output.flush();
                output.close();
                long end=System.currentTimeMillis();
                System.out.println("success用时:"+(end-start)/1000);
            }
           
         
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
           
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
           
        }
如题如示,怎么将本地F盘里的wo.mp3,上传到自己搭建的服务器上

解决方案 »

  1.   

    public class register extends ActionSupport {
    public File getUpload() {
    return upload;
    }
    public void setUpload(File upload) {
    this.upload = upload;
    }
    public String getUploadContentType() {
    return uploadContentType;
    }
    public void setUploadContentType(String uploadContentType) {
    this.uploadContentType = uploadContentType;
    }
    public String getUploadFileName() {
    return uploadFileName;
    }
    public void setUploadFileName(String uploadFileName) {
    this.uploadFileName = uploadFileName;
    }
    public String getSavePath() {
    return ServletActionContext.getRequest().getRealPath(savePath);
    }
    public void setSavePath(String savePath) {
    this.savePath = savePath;
    }
    private File upload;
    private String uploadContentType;
    private String uploadFileName;
    private String savePath;
    @Override
    public String execute() throws Exception {
    String filename=this.getSavePath()+"\\"+this.getUploadFileName();
    FileOutputStream fos=new FileOutputStream(filename);
    FileInputStream fis=new FileInputStream(this.getUpload());
    byte[] b=new byte[1024];
    int len=0;
    while((len=fis.read(b))>0){
    fos.write(b,0,len);

    }

    return SUCCESS;
    }
    }
    struts>
    <constant name="struts.multipart.saveDir" value="/tmp"/>
    <package name="struts2" extends="struts-default">
    <action name="up" class="com.struts2.register"><param name="savePath">/save</param>
    <result name="success">/Success.jsp</result>
    <result name="input">/Register.jsp</result>
    </action>
      

  2.   

    这样不可能行的,返回404,如果行得话,那让服务器端情何以堪
    你必须要考虑到服务端怎么接受这个文件,
    可以用FTP上传,如果服务器端开通FTP服务的话
    另外可以在服务器端开通一个JSP也行,用一些上传组件上传,比如org.apache.commons.fileupload
      

  3.   

    如果不使用框架的话,可以参考http://www.programfan.com/blog/article.asp?id=3673
      

  4.   

    http://www.cnblogs.com/lyn-x/articles/2070244.html