doufen3134 2015-09-22 21:04
浏览 76

Golang io.Copy()不会在CentOS中复制文件

I'm new to Go. The following code works perfectly in windows, but not on CentOS. I receive a multipart file from the client. The file has the correct size and there are no errors in reading it.

I need to create specific directories for the uploading user and save the files in there. Directories are created just fine, but the file is not written. This whole code runs with no errors on any stage. I'm using Go v1.5.1 and BeeGo framework.

Here's the code:

    file , header , err := this.GetFile( "logo" );

    defer func(){
        err := file.Close()
        if err != nil {
            fmt.Println( err.Error() )
        }
    }()


    size , seekErr := file.Seek( 0 , 2 )
    maxSize , _ := beego.AppConfig.Int64( "MaxSize" )

    if err != nil {

        response.Data = err.Error()
        response.Success = false

    } else if seekErr != nil {

        response.Data = seekErr.Error()
        response.Success = false

    } else if size > maxSize {

        response.Data = "File too large: " + string(size)
        response.Success = false

    } else {

        if _ , errDE := os.Stat( "static/media/business" ) ; os.IsNotExist( errDE ){
            os.Mkdir( "static/media/business" , os.ModeDir )
            os.Chmod( "static/media/business" , 0755 )
        }

        offset , err := file.Seek( 0 , 0 )

        if err != nil {
            fmt.Println( "Error seeking: " + err.Error() )
        }

        path := "static/media/business/" + u.( models.User ).Business.BusinessPage

        if _ , errDE := os.Stat( path ) ; os.IsNotExist( errDE ){
            os.Mkdir( path , os.ModeDir )
            os.Chmod( path , 0755 )
        }

        name := files.GenerateFileName( header.Filename , 24 )

        out , err := os.Create( path + "/" + name )

        if err != nil {

            response.Data = err.Error()
            response.Success = false

        } else {

            defer func(){
                err := out.Close()
                if err != nil {
                    fmt.Println( err.Error() )
                }
            }()

            _ , err = io.Copy( out , file )
            os.Chmod( path + "/" + name , 0755 )
            ...

Is there something I'm missing?

UPDATE

It appears that I can io.Copy to any *.txt file in the mentioned path, but not to an image file. Is that a clue?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?