最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

1、運行jar包,發(fā)送post請求

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請求必須設置如下兩行
? ? ? ? ? ? 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();
? ? ? ? ? ? // 寫上結尾標識
? ? ? ? ? ? out.write(end_data);
? ? ? ? ? ? out.flush();
? ? ? ? ? ? out.close();

? ? ? ? ? ? // 定義BufferedReader輸入流來讀取URL的響應
? ? ? ? ? ? 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請求出現(xiàn)異常!" + e);
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }

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

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("==> ?沒有上傳文件。");
? ? ? ? ? ? ? ? return Result.error("沒有上傳文件。");
? ? ? ? ? ? }
? ? ? ? ? ? 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測試成功。");
? ? ? ? logger.info("==> ?測試成功。");
? ? ? ? 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();
? ? }

}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Mybatis中注入執(zhí)行sql查詢、更新、新增及建表語句案例代碼

    Mybatis中注入執(zhí)行sql查詢、更新、新增及建表語句案例代碼

    這篇文章主要介紹了Mybatis中注入執(zhí)行sql查詢、更新、新增以及建表語句,主要說明一個另類的操作,注入sql,并使用mybatis執(zhí)行,結合案例代碼詳解講解,需要的朋友可以參考下
    2023-02-02
  • SpringBoot使用@Scheduled實現(xiàn)定時任務的并行執(zhí)行

    SpringBoot使用@Scheduled實現(xiàn)定時任務的并行執(zhí)行

    在SpringBoot中,如果使用@Scheduled注解來定義多個定時任務,默認情況下這些任務將會被安排在一個單線程的調(diào)度器中執(zhí)行,這意味著,這些任務將會串行執(zhí)行,而不是并行執(zhí)行,本文介紹了SpringBoot使用@Scheduled實現(xiàn)定時任務的并行執(zhí)行,需要的朋友可以參考下
    2024-06-06
  • spring boot配合前端實現(xiàn)跨域請求訪問

    spring boot配合前端實現(xiàn)跨域請求訪問

    本篇文章主要介紹了spring boot配合前端實現(xiàn)跨域請求訪問,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • java實現(xiàn)解析json復雜數(shù)據(jù)的第三種思路詳解

    java實現(xiàn)解析json復雜數(shù)據(jù)的第三種思路詳解

    這篇文章主要為大家信息介紹了java實現(xiàn)解析json復雜數(shù)據(jù)的第三種思路,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-01-01
  • Spring Boot 快速搭建微服務框架詳細教程

    Spring Boot 快速搭建微服務框架詳細教程

    SpringBoot是為了簡化Spring應用的創(chuàng)建、運行、調(diào)試、部署等而出現(xiàn)的,使用它可以做到專注于Spring應用的開發(fā),而無需過多關注XML的配置。本文重點給大家介紹Spring Boot 快速搭建微服務框架詳細教程,需要的的朋友參考下吧
    2017-09-09
  • 解讀JDK、JRE、JVM的區(qū)別與聯(lián)系

    解讀JDK、JRE、JVM的區(qū)別與聯(lián)系

    這篇文章主要介紹了解讀JDK、JRE、JVM的區(qū)別與聯(lián)系,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Jar包沖突問題原理及解決方案

    Jar包沖突問題原理及解決方案

    這篇文章主要介紹了Jar包沖突問題原理及解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • Java中的substring()方法使用舉例詳解

    Java中的substring()方法使用舉例詳解

    這篇文章主要介紹了Java中的substring()方法使用的相關資料,文中包括其概述、參數(shù)、返回值、使用示例、注意事項、常見用法和總結,通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-12-12
  • 理解java設計模式之建造者模式

    理解java設計模式之建造者模式

    這篇文章主要幫助大家理解java設計模式之建造者模式,對建造者模式,即生成器模式進行實例講解,感興趣的朋友可以參考一下
    2016-02-02
  • SpringBoot中通過實現(xiàn)WebMvcConfigurer參數(shù)校驗的方法示例

    SpringBoot中通過實現(xiàn)WebMvcConfigurer參數(shù)校驗的方法示例

    這篇文章主要介紹了SpringBoot中通過實現(xiàn)WebMvcConfigurer參數(shù)校驗的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-11-11

最新評論

鄂尔多斯市| 哈巴河县| 普兰店市| 仁寿县| 凤台县| 海伦市| 常熟市| 苍南县| 东阳市| 穆棱市| 昌图县| 基隆市| 蒲城县| 陆河县| 长汀县| 石家庄市| 灵石县| 台南县| 金阳县| 寻乌县| 秦皇岛市| 芒康县| 永修县| 温宿县| 天峨县| 瑞安市| 榕江县| 长宁区| 海南省| 新巴尔虎右旗| 富川| 富顺县| 台安县| 崇左市| 陇南市| 灌云县| 万盛区| 巴里| 湘阴县| 错那县| 故城县|