• Go实现上传下载功能


    `
    package main

    import (
    "crypto/md5"
    "encoding/hex"
    "fmt"
    "io"
    "io/ioutil"
    "log"
    "mime"
    "net/http"
    "path/filepath"
    )
    const saveDir = "./uploads"
    const maxUploadSize = 1024 * 1024 * 1024 * 2

    func ApiErrorResult(writer io.Writer, code int , msg string) {
    s := fmt.Sprintf({"code": %d, "msg": %s}, code, msg)
    _, _ = writer.Write([]byte(s))

    }

    func ApiResult(writer io.Writer, code int , msg string) {
    s := fmt.Sprintf({"code": %d, "msg": %s}, code, msg)
    _, _ = writer.Write([]byte(s))

    }

    func handleFileUpload(resp http.ResponseWriter, req *http.Request) {
    // 设置最大读取
    req.Body = http.MaxBytesReader(resp, req.Body, maxUploadSize)
    if err := req.ParseMultipartForm(maxUploadSize); err != nil {
    ApiErrorResult(resp,-1, "上传文件太大")
    return
    }

    uploadFile, _, err := req.FormFile("file")
    if err != nil {
    	ApiErrorResult(resp, -1, err.Error())
    	return
    }
    var fileType string
    fileData, err := ioutil.ReadAll(uploadFile);
    if err != nil {
    	ApiErrorResult(resp,-1, err.Error())
    	return
    } else {
    	// 从文件中读取类型
    	fileType = http.DetectContentType(fileData)
    }
    // 检测是否支持的类型
    fileEndings, err := mime.ExtensionsByType(fileType)
    if err != nil {
    	ApiErrorResult(resp,-1, err.Error())
    	return
    }
    if len(fileEndings) == 0 {
    	ApiErrorResult(resp,-1, "不支持此文件类型上传")
    	return
    }
    fileMd5 := md5.New()
    fileMd5.Write(fileData)
    fileName := filepath.Join(saveDir,hex.EncodeToString(fileMd5.Sum(nil))+ fileEndings[0])
    if err := ioutil.WriteFile(fileName, fileData, 0777); err == nil {
    	ApiResult(resp, 0, "上传成功")
    	return
    } else {
    	ApiErrorResult(resp, -1, err.Error())
    }
    

    }
    func main() {
    http.HandleFunc("/upload", handleFileUpload)
    http.Handle("/files/", http.StripPrefix("/files/", http.FileServer(http.Dir(saveDir))))
    log.Print("正在监听:8080服务")
    http.ListenAndServe(":8080", nil)
    }
    `

  • 相关阅读:
    mybatis中 无效的比较: invalid comparison: java.util.Date and java.lang.String
    java中日期的加减,比较,以及与String的互相转换
    Java中文件上传下载 --使用Minio
    Dubbo SPI 源码深入分析
    新建Maven项目 发布服务 注册到zookeeper
    Dubbo SPI 源码分析
    思维方法
    JDK 和 Dubbo SPI 的入门浅析Demo
    Dubbo不用注册中心实现远程调用的简单用法demo
    解决linux系统下tar解压文件报错问题
  • 原文地址:https://www.cnblogs.com/pengshaomin/p/13081047.html
Copyright © 2020-2023  润新知