Fastdfs处理文件上传
1,后台控制层代码
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import xusage.crm.utils.Operation;
import xusage.crm.utils.UpLoadFilesUtil;
@Operation(name="文件管理接口")
@Controller
@RequestMapping("file")
public class FileController {
private UpLoadFilesUtil upLoadFilesUtil=new UpLoadFilesUtil();
@Operation(name="上传客户相关文件")
@RequestMapping("upload")
@ResponseBody
public Map<String, Object> fileUpload(HttpServletRequest request,MultipartFile files) throws Exception{
Map<String, Object> map = new HashMap<String,Object>(16);
//判断文件的上传是否为空
//System.err.println(files);
if(files == null || files.isEmpty()) {
map.put("status", false);
map.put("code", -1);
map.put("msg","上传文件为空");
return map;
}
//调用工具类中的upLoadFilesUtil方法,对文件进行上传操作,将文件的信息存储到map里
map = upLoadFilesUtil.upLoadUtil(map, files);
System.err.println(map.get("id"));
//对得到的文件map信息进行转json字符串操作
//String aff = json.writeValueAsString(affixes);
return map;
}
}
先判断是否为文件上传操作,若是,则调用文件上传工具类UpLoadFilesUtil.java
2,调用文件上传
import java.io.File;
import java.util.Map;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import com.alibaba.fastjson.JSONObject;
public class UpLoadFilesUtil {
private MultToFile multToFile=new MultToFile();
public Map<String,Object> upLoadUtil(Map<String,Object> map,MultipartFile file)throws Exception{
//对文件进行类型的转换,得到文件的路径
CommonsMultipartFile cf= (CommonsMultipartFile)file;
DiskFileItem fi = (DiskFileItem)cf.getFileItem();
File f = fi.getStoreLocation();
String path = f.getCanonicalPath();
//文件上传。处理文件
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
//fastdfs上传的路径
String uri = "";
HttpPost httppost = new HttpPost(uri);
//File file = new File(path);
//通过文件路径得到文件,并转为FileBody类型
FileBody bin = new FileBody(new File(path));
StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("file", bin)
.addPart("comment", comment)
.build();
httppost.setEntity(reqEntity);
System.out.println("executing request " + httppost.getRequestLine());
CloseableHttpResponse response2 = httpclient.execute(httppost);
//获取长上传文件后的返回的id。
HttpEntity entity = response2.getEntity();
String mes = EntityUtils.toString(entity, "UTF-8");
JSONObject jsonObject = JSONObject.parseObject(mes);
String id = jsonObject.getString("id");
//通过返回的ID拼接访问路径,来查询文件
String httpurl=""+id;
map.put("httpurl", httpurl);
//map.put("response", mes);
try {
System.out.println(response2.getStatusLine());
HttpEntity resEntity = response2.getEntity();
if (resEntity != null) {
System.out.println("Response content length: " + resEntity.getContentLength());
}
EntityUtils.consume(resEntity);
} finally {
response2.close();
}
//将返回的ID存入map
map.put("id",id);
} finally {
httpclient.close();
}
//文件大小
map.put("fileSize", file.getSize());
//文件名
map.put("fileName", file.getOriginalFilename());
//文件类型
map.put("type", file.getContentType());
return map;
}
}