亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java實(shí)現(xiàn)文件上傳到服務(wù)器

 更新時(shí)間:2022年06月23日 09:44:41   作者:皮卡丘的搬磚日記  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)文件上傳到服務(wù)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java實(shí)現(xiàn)文件上傳到服務(wù)器的具體代碼,供大家參考,具體內(nèi)容如下

1、運(yùn)行jar包,發(fā)送post請(qǐng)求

public static void main(String[] args) {

? ? ? ? //String filePath="C:/Users/706293/IT_onDuty.xls";
? ? ? ? String filePath=args[0];
? ? ? ? String unid=args[1];
? ? ? ?// String unid="155555";
? ? ? ? DataOutputStream out = null;
? ? ? ? final String newLine = "\r\n";
? ? ? ? final String prefix = "--";
? ? ? ? try {
? ? ? ? ? ? URL url = new URL("http://172.20.200.64:9000/excel9000/uploads");
? ? ? ? ? ? HttpURLConnection conn = (HttpURLConnection)url.openConnection();

? ? ? ? ? ? String BOUNDARY = "-------7da2e536604c8";
? ? ? ? ? ? conn.setRequestMethod("POST");
? ? ? ? ? ? // 發(fā)送POST請(qǐng)求必須設(shè)置如下兩行
? ? ? ? ? ? conn.setDoOutput(true);
? ? ? ? ? ? conn.setDoInput(true);
? ? ? ? ? ? conn.setUseCaches(false);
? ? ? ? ? ? conn.setRequestProperty("connection", "Keep-Alive");
? ? ? ? ? ? conn.setRequestProperty("Charsert", "UTF-8");
? ? ? ? ? ? conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);

? ? ? ? ? ? out = new DataOutputStream(conn.getOutputStream());

? ? ? ? ? ? // 添加參數(shù)file
? ? ? ? ? ? File file = new File(filePath);
? ? ? ? ? ? StringBuilder sb1 = new StringBuilder();
? ? ? ? ? ? sb1.append(prefix);
? ? ? ? ? ? sb1.append(BOUNDARY);
? ? ? ? ? ? sb1.append(newLine);
? ? ? ? ? ? sb1.append("Content-Disposition: form-data;name=\"file\";filename=\"" + file.getName() + "\"" + newLine);
? ? ? ? ? ? sb1.append("Content-Type:application/octet-stream");
? ? ? ? ? ? sb1.append(newLine);
? ? ? ? ? ? sb1.append(newLine);
? ? ? ? ? ? out.write(sb1.toString().getBytes());
? ? ? ? ? ? DataInputStream in = new DataInputStream(new FileInputStream(file));
? ? ? ? ? ? byte[] bufferOut = new byte[1024];
? ? ? ? ? ? int bytes = 0;
? ? ? ? ? ? while ((bytes = in.read(bufferOut)) != -1) {
? ? ? ? ? ? ? ? out.write(bufferOut, 0, bytes);
? ? ? ? ? ? }
? ? ? ? ? ? out.write(newLine.getBytes());
? ? ? ? ? ? in.close();

? ? ? ? ? ? // 添加參數(shù)sysName
? ? ? ? ? ? StringBuilder sb = new StringBuilder();
? ? ? ? ? ? sb.append(prefix);
? ? ? ? ? ? sb.append(BOUNDARY);
? ? ? ? ? ? sb.append(newLine);
? ? ? ? ? ? sb.append("Content-Disposition: form-data;name=\"unid\"");
? ? ? ? ? ? sb.append(newLine);
? ? ? ? ? ? sb.append(newLine);
? ? ? ? ? ? sb.append(unid);
? ? ? ? ? ? out.write(sb.toString().getBytes());

? ? ? ? ? ? ?添加參數(shù)returnImage
? ? ? ? ? ? //StringBuilder sb2 = new StringBuilder();
? ? ? ? ? ? //sb2.append(newLine);
? ? ? ? ? ? //sb2.append(prefix);
? ? ? ? ? ? //sb2.append(BOUNDARY);
? ? ? ? ? ? //sb2.append(newLine);
? ? ? ? ? ? //sb2.append("Content-Disposition: form-data;name=\"returnImage\"");
? ? ? ? ? ? //sb2.append(newLine);
? ? ? ? ? ? //sb2.append(newLine);
? ? ? ? ? ? //sb2.append("false");
? ? ? ? ? ? //out.write(sb2.toString().getBytes());

? ? ? ? ? ? byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
? ? ? ? ? ? // 寫上結(jié)尾標(biāo)識(shí)
? ? ? ? ? ? out.write(end_data);
? ? ? ? ? ? out.flush();
? ? ? ? ? ? out.close();

? ? ? ? ? ? // 定義BufferedReader輸入流來(lái)讀取URL的響應(yīng)
? ? ? ? ? ? BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
? ? ? ? ? ? String line = null;
? ? ? ? ? ? while ((line = reader.readLine()) != null) {
? ? ? ? ? ? ? ? System.out.println(line);
? ? ? ? ? ? }

? ? ? ? } catch (Exception e) {
? ? ? ? ? ? System.out.println("發(fā)送POST請(qǐng)求出現(xiàn)異常!" + e);
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }

2、服務(wù)器接收端,將文件上床服務(wù)器指定位置

package com.dayang.ExcelController;


import com.dayang.ExcelService.FileService;
import com.dayang.dubbo.CreateExcelConsumer;
import com.dayang.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class FileController {

? ? protected static final Logger logger = LoggerFactory.getLogger(FileController.class);

? ? @Autowired
? ? private CreateExcelConsumer createExcelConsumer;
? ? @Autowired
? ? FileService fileService;

? ? @PostMapping("/uploads")
? ? public String uploads(@RequestParam("file") MultipartFile file,@RequestParam("unid")String unid) throws IOException {
? ? ? ? //String unid="444444";
? ? ? ? String uploadPath = "";
? ? ? ? try{
? ? ? ? ? ? logger.info("==>uuid: " + unid);
? ? ? ? ? ? if (file == null) {
? ? ? ? ? ? ? ? logger.error("==> ?沒(méi)有上傳文件。");
? ? ? ? ? ? ? ? return Result.error("沒(méi)有上傳文件。");
? ? ? ? ? ? }
? ? ? ? ? ? logger.info("==>文件名: " + file.getOriginalFilename());
? ? ? ? ? ? ?uploadPath = fileService.handlerMultipartFile(file,unid);
? ? ? ? ? ? //return Result.success("文件上傳完成。", newFileName);
? ? ? ? ? ? //uploadPath = createExcelConsumer.uploadExcel(file,unid);
? ? ? ? ? ? logger.info("==>文件路徑: " + uploadPath);
? ? ? ? }
? ? ? ? catch (Exception e){

? ? ? ? }

? ? ? ? return uploadPath;

? ? }
? ? @RequestMapping("/test")
? ? public ?String ?test(){
? ? ? ? System.out.println("test測(cè)試成功。");
? ? ? ? logger.info("==> ?測(cè)試成功。");
? ? ? ? return ?"test";
? ? }

}

3、service

package com.dayang.ExcelService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

@Service
public class FileService {

? ? protected static final Logger logger= LoggerFactory.getLogger(FileService.class);

? ? private String directoryPath = "C:\\Temp";

? ? public FileService() {
? ? ? ? File directory = new File(directoryPath);
? ? ? ? if (!directory.exists()) {
? ? ? ? ? ? directory.mkdirs();
? ? ? ? }
? ? }

? ? public String handlerMultipartFile(MultipartFile multipartFile ,String unid) {
? ? ? ? String fileOldName = multipartFile.getOriginalFilename();
? ? ? ? int beginIndex = fileOldName.lastIndexOf(".");
? ? ? ?String suffix = fileOldName.substring(beginIndex);
? ? ? ? String newFileName = ?unid+ suffix;
? ? ? ? File upFile = new File(directoryPath + "/" + newFileName);
? ? ? ? OutputStream outputStream = null;
? ? ? ? try {
? ? ? ? ? ? byte[] fileByte = multipartFile.getBytes();
? ? ? ? ? ? outputStream = new FileOutputStream(upFile);
? ? ? ? ? ? outputStream.write(fileByte);
? ? ? ? ? ? logger.info("<== ?文件寫出完成: " + newFileName);
? ? ? ? ? ? return newFileName;
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? logger.error("", e);
? ? ? ? } finally {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? if (outputStream != null) {
? ? ? ? ? ? ? ? ? ? outputStream.flush();
? ? ? ? ? ? ? ? ? ? outputStream.close();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? } catch (Exception e) {
? ? ? ? ? ? ? ? logger.error("", e);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return directoryPath + "/" + newFileName;
? ? }

}

4、Result

package com.dayang.util;

import com.alibaba.fastjson.JSONObject;

public class Result {

? ? public static String success(String msg, Object result) {
? ? ? ? JSONObject jsonObject = new JSONObject();
? ? ? ? jsonObject.put("status", 1);
? ? ? ? jsonObject.put("message", msg);
? ? ? ? jsonObject.put("result", result);
? ? ? ? return jsonObject.toJSONString();
? ? }

? ? public static String error(String msg) {
? ? ? ? JSONObject jsonObject = new JSONObject();
? ? ? ? jsonObject.put("status", -1);
? ? ? ? jsonObject.put("message", msg);
? ? ? ? return jsonObject.toJSONString();
? ? }

}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java常用類庫(kù)Apache Commons工具類說(shuō)明及使用實(shí)例詳解

    Java常用類庫(kù)Apache Commons工具類說(shuō)明及使用實(shí)例詳解

    這篇文章主要介紹了Java常用類庫(kù)Apache Commons工具類說(shuō)明及使用實(shí)例詳解,需要的朋友可以參考下
    2020-02-02
  • Java字符串拼接的優(yōu)雅方式實(shí)例詳解

    Java字符串拼接的優(yōu)雅方式實(shí)例詳解

    字符串拼接一般使用“+”,但是“+”不能滿足大批量數(shù)據(jù)的處理,下面這篇文章主要給大家介紹了關(guān)于Java字符串拼接的幾種優(yōu)雅方式,需要的朋友可以參考下
    2021-07-07
  • Javaweb基礎(chǔ)入門HTML之table與form

    Javaweb基礎(chǔ)入門HTML之table與form

    HTML的全稱為超文本標(biāo)記語(yǔ)言,是一種標(biāo)記語(yǔ)言。它包括一系列標(biāo)簽.通過(guò)這些標(biāo)簽可以將網(wǎng)絡(luò)上的文檔格式統(tǒng)一,使分散的Internet資源連接為一個(gè)邏輯整體。HTML文本是由HTML命令組成的描述性文本,HTML命令可以說(shuō)明文字,圖形、動(dòng)畫、聲音、表格、鏈接等
    2022-03-03
  • mybatis通過(guò)TypeHandler?list轉(zhuǎn)換string類型轉(zhuǎn)換方式

    mybatis通過(guò)TypeHandler?list轉(zhuǎn)換string類型轉(zhuǎn)換方式

    這篇文章主要介紹了mybatis通過(guò)TypeHandler?list轉(zhuǎn)換string類型轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 兩種實(shí)現(xiàn)Java類隔離加載的方法

    兩種實(shí)現(xiàn)Java類隔離加載的方法

    這篇文章主要介紹了兩種實(shí)現(xiàn)Java類隔離加載的方法,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-02-02
  • SpringBoot如何使用Scala進(jìn)行開(kāi)發(fā)的實(shí)現(xiàn)

    SpringBoot如何使用Scala進(jìn)行開(kāi)發(fā)的實(shí)現(xiàn)

    這篇文章主要介紹了SpringBoot如何使用Scala進(jìn)行開(kāi)發(fā)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Java面向?qū)ο笾蓡T隱藏與屬性封裝操作示例

    Java面向?qū)ο笾蓡T隱藏與屬性封裝操作示例

    這篇文章主要介紹了Java面向?qū)ο笾蓡T隱藏與屬性封裝操作,結(jié)合實(shí)例形式分析了Java面向?qū)ο蟪绦蛟O(shè)計(jì)中成員的隱藏及屬性封裝相關(guān)實(shí)現(xiàn)與使用操作技巧,需要的朋友可以參考下
    2018-06-06
  • Springboot整合activemq的方法步驟

    Springboot整合activemq的方法步驟

    這篇文章主要介紹了Springboot整合activemq的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • Java使用JMeter進(jìn)行高并發(fā)測(cè)試

    Java使用JMeter進(jìn)行高并發(fā)測(cè)試

    軟件的壓力測(cè)試是一種保證軟件質(zhì)量的行為,本文主要介紹了Java使用JMeter進(jìn)行高并發(fā)測(cè)試,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Java如何優(yōu)雅地避免空指針異常(NullPointerException)

    Java如何優(yōu)雅地避免空指針異常(NullPointerException)

    這篇文章主要給大家介紹了關(guān)于Java如何優(yōu)雅地避免空指針異常(NullPointerException)的相關(guān)資料,空指針異常(NullPointerException)是一種常見(jiàn)的運(yùn)行時(shí)異常,它在Java編程中經(jīng)常出現(xiàn),需要的朋友可以參考下
    2024-03-03

最新評(píng)論