UploadUtil导入工具类

package com.common.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class UploadUtil {

    private static String uploadPath;

    @Value("${upload.path}")
    public void setUploadPath(String uploadPath) {
        UploadUtil.uploadPath = uploadPath;
    }

    /**
     * 上传文件
     *
     * @param applicationName
     * @param file
     * @return
     */
    public static Map upload(String applicationName, MultipartFile file) {
        Map map = new HashMap();
        String fileName = file.getOriginalFilename();
        map.put("name", fileName);

        String type = fileName.substring(fileName.lastIndexOf("."));
        fileName = new Date().getTime() + type;
        File dest = new File(uploadPath + applicationName + "/" + fileName);
        if (!dest.getParentFile().exists()) { // 判断文件父目录是否存?
            dest.getParentFile().mkdirs();
        }
        map.put("type", type);
        map.put("filePath", applicationName + "/" + fileName);
        map.put("localFilePath", uploadPath + applicationName + "/" + fileName);

        try {
            file.transferTo(dest); // 保存文件
        } catch (IllegalStateException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return map;
    }


    /**
     * 复制单个文件
     *
     * @param oldPath String 原文件路? 如:c:/fqf.txt
     * @param newPath String 复制后路? 如:f:/fqf.txt
     * @return boolean
     */
    @SuppressWarnin
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!很高兴为你解答问题。以下是使用Java实现上传文件至服务器的代码示例: ``` import java.io.File; import java.io.IOException; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.FileRequestEntity; import org.apache.commons.httpclient.methods.PostMethod; public class UploadUtil { /** * 上传文件至服务器 * @param file 需要上传的文件 * @param url 服务器接收文件的地址 * @return 返回上传是否成功的标记 */ public static boolean uploadFile(File file, String url) { boolean success = false; PostMethod postMethod = new PostMethod(url); HttpClient client = new HttpClient(); try { FileRequestEntity fileRequestEntity = new FileRequestEntity(file,"application/octet-stream"); postMethod.setRequestEntity(fileRequestEntity); int statusCode = client.executeMethod(postMethod); if (statusCode == HttpStatus.SC_OK) { success = true; System.out.println("上传成功!"); } } catch (IOException e) { e.printStackTrace(); } finally { postMethod.releaseConnection(); } return success; } /** * 示例 * @param args */ public static void main(String[] args) { File file = new File("/Users/test/test.txt"); // 需要上传到服务器的文件路径 String url = "http://localhost:8080/test/upload"; // 服务器接收文件的地址 uploadFile(file,url); // 上传文件至服务器 } } ``` 希望能够帮助到你!如果有任何问题,欢迎继续向我提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值