品优购中的FastDFS文件上传

FastDFS图片服务器下载:

链接:https://pan.baidu.com/s/111Zu5KJXUEqOQk8E_xmqvw
提取码:xphg
下载完之后,解压,运行.vmx文件,挂到虚拟机上,网络适配器一定要改为192.168.25.xx端口
用户名:root
密码:itcast

一、pinyougou-common

在工具类中加入FastDFSClient.java工具类

FastDFSClient.java

package util;
 
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
 
public class FastDFSClient {
 
	private TrackerClient trackerClient = null;
	private TrackerServer trackerServer = null;
	private StorageServer storageServer = null;
	private StorageClient1 storageClient = null;
	
	public FastDFSClient(String conf) throws Exception {
		if (conf.contains("classpath:")) {
			conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
		}
		ClientGlobal.init(conf);
		trackerClient = new TrackerClient();
		trackerServer = trackerClient.getConnection();
		storageServer = null;
		storageClient = new StorageClient1(trackerServer, storageServer);
	}
	
	/**
	 * 上传文件方法
	 * <p>Title: uploadFile</p>
	 * <p>Description: </p>
	 * @param fileName 文件全路径
	 * @param extName 文件扩展名,不包含(.)
	 * @param metas 文件扩展信息
	 * @return
	 * @throws Exception
	 */
	public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
		String result = storageClient.upload_file1(fileName, extName, metas);
		return result;
	}
	
	public String uploadFile(String fileName) throws Exception {
		return uploadFile(fileName, null, null);
	}
	
	public String uploadFile(String fileName, String extName) throws Exception {
		return uploadFile(fileName, extName, null);
	}
	
	/**
	 * 上传文件方法
	 * <p>Title: uploadFile</p>
	 * <p>Description: </p>
	 * @param fileContent 文件的内容,字节数组
	 * @param extName 文件扩展名
	 * @param metas 文件扩展信息
	 * @return
	 * @throws Exception
	 */
	public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception {
		
		String result = storageClient.upload_file1(fileContent, extName, metas);
		return result;
	}
	
	public String uploadFile(byte[] fileContent) throws Exception {
		return uploadFile(fileContent, null, null);
	}
	
	public String uploadFile(byte[] fileContent, String extName) throws Exception {
		return uploadFile(fileContent, extName, null);
	}
}

在pom.xml中引入配置
		<dependency>
		    <groupId>org.csource.fastdfs</groupId>
		    <artifactId>fastdfs</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
		</dependency>

二、pinyougou-shop-web

在这里插入图片描述

springmvc.xml

引入application.properties

<context:property-placeholder location="classpath:config/application.properties" />

配置多媒体解析器

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
			<property name="defaultEncoding" value="UTF-8"></property>
			<!-- 设定文件上传的最大值5MB,5*1024*1024 -->
			<property name="maxUploadSize" value="5242880"></property>
</bean>
application.properties

键值对 值为FastDFS图片服务器的地址

FILE_SERVER_URL=HTTP://192.168.25.133/
fdfs_client.conf

tracker_server为服务器地址,默认端口22122

connect_timeout=30
network_timeout=60
base_path=/home/fastdfs
tracker_server=192.168.25.133:22122
log_level=info
use_connection_pool = false
connection_pool_max_idle_time = 3600
load_fdfs_parameters_from_tracker=false
use_storage_id = false
storage_ids_filename = storage_ids.conf
http.tracker_server_port=80
UploadController.java

后端代码:

package com.pinyougou.shop.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import entity.Result;
import util.FastDFSClient;

@RestController
public class UploadController {
	//首先在springmvc里边引入application.properties,然后application.properties的键值对中的键就是value.用$括起来
	@Value("${FILE_SERVER_URL}")
	private String file_server_url;
	
	@RequestMapping("/upload")
	public Result upload(MultipartFile file) {
		//获取文件全名称
		String filename = file.getOriginalFilename();
		//获取.后边的文件类型 扩展名     从逗号开始截取,但是这样会带逗号,所以在加一
		String type = filename.substring(filename.lastIndexOf(".")+1);
		try {
			//使用的是common中的FastDFSClient.java工具类,需要在web的pom中引入common才可以使用,传入参数:配置文件的路径
			FastDFSClient client = new FastDFSClient("calsspath:config/fdfs_client.conf");
			String fileId = client.uploadFile(file.getBytes(), type);
			//图片的完整地址
			String url = file_server_url+fileId;
			System.out.println(url);
			//保存成功 返回url到前端
			return new Result(true, url);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Result(true, "上传失败");
		} 
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值