In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to upload integrated files to FastDFS by Java, which is very detailed and has certain reference value. Friends who are interested must finish reading it!
1. Introduce fastdfs dependency to pom.xml com.github.tobato fastdfs-client 1.26.52. The upload code is as follows: upload pure file stream / * file upload * @ param file MultipartFile type * @ return url * / @ Override public String fileUpload (MultipartFile file) throws Exception {try {return upload (file);} catch (Exception e) {e.printStackTrace ();} throw new Exception () Link to upload network resources: / * File upload * @ param urlStr url address * @ return url * / @ Override public String fileUpload (String urlStr) throws Exception {try {/ / convert the address to URL object URL url = new URL (urlStr) / / create http link HttpURLConnection conn = (HttpURLConnection) url.openConnection (); / / set timeout to 3 seconds conn.setConnectTimeout (3x1000); / / prevent shielding program from crawling and return 403 error conn.setRequestProperty ("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64)" X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36)); / / get the input stream InputStream inputStream = conn.getInputStream (); / / intercept the file name in the link String fileName= urlStr.substring (urlStr.lastIndexOf ("/") + 1); MultipartFile multipartFile = new MockMultipartFile (fileName,fileName, ContentType.APPLICATION_OCTET_STREAM.toString (), inputStream) / / return result set return upload (multipartFile);} catch (Exception e) {e.printStackTrace ();} throw new Exception ();} the whole code is as follows: package com.tfjybj.arpro.crawl.service.impl; import com.github.tobato.fastdfs.domain.fdfs.StorePath;import com.github.tobato.fastdfs.service.FastFileStorageClient;import com.tfjybj.arpro.crawl.service.FileUploadService Import com.tfjybj.arpro.crawl.util.CommonConfigurationUtil;import lombok.extern.slf4j.Slf4j;import org.apache.commons.lang3.StringUtils;import org.apache.http.entity.ContentType;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.mock.web.MockMultipartFile;import org.springframework.stereotype.Service;import org.springframework.web.multipart.MultipartFile; import java.io.*;import java.net.HttpURLConnection;import java.net.URL / * * File upload business class * * @ author Promsing (Zhang Youbo) * @ version 1.0.0 * @ since 2022-2-25-20:01 * / @ Service@Slf4jpublic class FileUploadServiceImpl implements FileUploadService {@ Autowired private FastFileStorageClient fastFileStorageClient; / / obtain the configuration IP address @ Value ("${fdfs.realIp}") private String realIp in the configuration file / / get the configuration grouping @ Value ("${fdfs.groupName}") private String group; / * File upload * @ param file MultipartFile * @ return url * / @ Override public String fileUpload (MultipartFile file) throws Exception {try {return upload (file) in the configuration file } catch (Exception e) {e.printStackTrace ();} throw new Exception () } / * File upload * @ param urlStr url address * @ return url * / @ Override public String fileUpload (String urlStr) throws Exception {try {/ / convert address to URL object URL url = new URL (urlStr); / / create http link HttpURLConnection conn = (HttpURLConnection) url.openConnection () / / set the timeout to 3 seconds conn.setConnectTimeout (3x1000); / / return conn.setRequestProperty ("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36") while preventing the screen from crawling. / / get the input stream InputStream inputStream = conn.getInputStream (); / / intercept the file name in the link String fileName= urlStr.substring (urlStr.lastIndexOf ("/") + 1); MultipartFile multipartFile = new MockMultipartFile (fileName,fileName, ContentType.APPLICATION_OCTET_STREAM.toString (), inputStream); / / return the result set return upload (multipartFile) } catch (Exception e) {e.printStackTrace ();} throw new Exception ();} / * * File upload * @ param file File to upload * @ return File address after upload * / public String upload (MultipartFile file) {try {/ / 1. File information verification if (file.isEmpty ()) {log.debug ("File information to be uploaded does not pass"); return null;} / / 2. Save the picture to the fastDFS server / / 2.1get the file suffix String extension = StringUtils.substringAfterLast (file.getOriginalFilename (), "."); / / 2.2 Save StorePath storePath = fastFileStorageClient.uploadFile (group, file.getInputStream (), file.getSize (), extension) / / get the full address of the attachment String Path = CommonConfigurationUtil.HTTP + CommonConfigurationUtil.ECOLON + CommonConfigurationUtil.DOUBLE_SLASH + realIp + CommonConfigurationUtil.SINGLE_SLASH + storePath.getFullPath (); log.info ("File uploaded successfully, file address:" + Path); return Path;} catch (Exception ex) {ex.printStackTrace (); return null } 3. The configuration file is as follows # File server basic configuration fdfs: groupName: ar so-timeout: 1500 connect-timeout: 600tracker-list: d-fastdfs.xxxx.com:22122 replace-ip: source: d-fastdfs.xxxx.com dest: d-fastdfs.xxxx.com realIp: d-fastdfs.xxxx.com4. The upload effect is as follows
Whether it is pure file upload or upload in the form of links to network resources, it is in the form of file stream upload.
The above is all the contents of the article "how to upload integrated files to FastDFS by Java". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.