淺談
我一直都覺得上傳圖片好復雜,除了本地上傳,還有局域網上傳,公網上傳亂七八糟的,不僅看不懂,還不想學,因為老是覺得本地上傳沒啥大用處,直到今天,我才看透,什么本地不本地的,統統都是一個套路!
在springboot2.×版本以后,上傳時就不需要任何配置了,什么配置文件也不需要,啥也不講了,上來就是干!
首先來一波IO
流的基本操作
本地創建一個文件 向里面寫入內容
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/** 在本地新建一個文件夾 里面創建一個文件 向里面寫入內容 */
//1. 文件夾的路徑 文件名
String directory = "E:\\test";
String filename = "test.txt";
//2. 創建文件夾對象 創建文件對象
File file = new File(directory);
//如果文件夾不存在 就創建一個空的文件夾
if (!file.exists()) {
file.mkdirs();
}
File file2 = new File(directory, filename);
//如果文件不存在 就創建一個空的文件
if (!file2.exists()) {
try {
file2.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//3.寫入數據
//創建文件字節輸出流
FileOutputStream fos = new FileOutputStream(directory + "\\" + filename);
//開始寫
String str = "測試數據";
byte[] bytes = str.getBytes();
//將byte數組中的所有數據全部寫入
fos.write(bytes);
//關閉流
fos.close();
讀取本地一個文件中的內容 寫入另一個文件
import java.io.FileInputStream;
import java.io.FileOutputStream;
/** 讀取本地一個文件中的內容 寫入另一個文件 */
//創建文件字節輸入流 這個路徑下的文件必須存在
FileInputStream fis = new FileInputStream("E:\\test\\test.txt");
//創建文件字節輸出流 如果這個文件不存在 會自動創建一個
FileOutputStream fos = new FileOutputStream("E:\\test\\test01.txt");
//一邊讀一邊寫
byte[] bytes = new byte[1024];
int temp = 0;
while ((temp = fis.read(bytes)) != -1) {
//將byte數組中內容直接寫入
fos.write(bytes, 0, temp);
}
//刷新
fos.flush();
//關閉
fis.close();
fos.close();
1,FileWritter寫入文件
FileWritter, 字符流寫入字符到文件。默認情況下,它會使用新的內容取代所有現有的內容,然而,當指定一個true (布爾)值作為FileWritter構造函數的第二個參數,它會保留現有的內容,並追加新內容在文件的末尾。
示例:
String url="D:\\test.txt";
File file=new File(url);
if(!file.exists()){
file.createNewFile();
}
FileWriter fileWriter=null;
BufferedWriter bufferedWriter=null;
try {
fileWriter=new FileWriter(file,true);
bufferedWriter=new BufferedWriter(fileWriter);
bufferedWriter.write(list.toString());
} catch (IOException e) {
e.printStackTrace();
}finally {
bufferedWriter.close();
osBufferedWriter.close();
}
2,FileOutputStream寫入文件
文件輸出流是一種用於處理原始二進制數據的字節流類。為了將數據寫入到文件中,必須將數據轉換為字節,並保存到文件。請參閱下面的完整的例子。
File file = new File("c:/newfile.txt");
String content = "This is the text content";
try (FileOutputStream fop = new FileOutputStream(file)) {
if (!file.exists()) {
file.createNewFile();
}
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
} catch (IOException e) {
e.printStackTrace();
}
下面是我自己做個一個商城項目上傳圖片的demo
首先是數據庫表
這個項目使用的是springboot,mybatis,thymeleaf
前台html頁面代碼
<div id="div">
<form action="/demo/upload" method="post" enctype="multipart/form-data">
上架的商品名稱:<input class="input" type="text" name="name"><br>
商品尺寸:<select class="input" name="size">
<option value="M">M</option>
<option value="L">L</option>
<option value="XL">XL</option>
<option value="XXL">XXL</option>
</select><br>
商品價格:<input class="input" type="text" name="price"><br>
上架商品數量:<input class="input" type="text" name="number"><br>
商品顏色:<select class="input" name="color">
<option value="藍色">藍色</option>
<option value="白色">白色</option>
<option value="黑色">黑色</option>
<option value="灰色">灰色</option>
</select><br>
商品類型:<select class="input" name="kind">
<option value="男裝">男裝</option>
<option value="女裝">女裝</option>
<option value="童裝">童裝</option>
<option value="時尚包包">時尚包包</option>
</select><br>
原價:<input class="input" type="text" name="preprice"><br>
商品圖片:<input class="input" type="file" name="pic"><br>
上架日期:<input class="input" type="date" name="time"><br>
<input type="submit" value="確認上架">
</form>
</div>
controller層代碼
/**
* 商品圖片上傳
*/
@RequestMapping("/upload")
public String upload(@RequestParam(value = "pic") MultipartFile pic,@RequestParam Map param,Model model) throws ParseException {
System.err.println("傳過來的值"+param);
if(pic.isEmpty()){
System.err.println("上傳文件不可為空");
}
String fileName=pic.getOriginalFilename();//得到文件名
String suffixName=fileName.substring(fileName.lastIndexOf("."));//得到后綴名
System.err.println("suffixName:"+suffixName);
String filepath="D:/tempFiles/files/";//指定圖片上傳到哪個文件夾的路徑
fileName= UUID.randomUUID()+suffixName;//重新命名圖片,變成隨機的名字
System.err.println("fileName:"+fileName);
File dest=new File(filepath+fileName);//在上傳的文件夾處創建文件
try {
pic.transferTo(dest);//把上傳的圖片寫入磁盤中
} catch (IOException e) {
e.printStackTrace();
}
//到這里為止,下面的都不用再看了,跟上傳沒關系,都是實體類傳值竟然麻煩成這個樣子
Shangpin shangpin=new Shangpin();
//這里就不想改了,把fileName當成picpath傳過去算了
String picpath=fileName;
shangpin.setPicpath(picpath);
shangpin.setName((String) param.get("name"));
shangpin.setSize((String) param.get("size"));
double price=Double.parseDouble(param.get("price").toString());
shangpin.setPrice(price);
shangpin.setNumber(Integer.valueOf(param.get("number").toString()));
shangpin.setColor((String) param.get("color"));
double preprice=Double.parseDouble(param.get("preprice").toString());
shangpin.setPreprice(preprice);
shangpin.setKind((String) param.get("kind"));
String sellerAccount=phone;
shangpin.setSellerAccount(sellerAccount);
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
shangpin.setTime(simpleDateFormat.parse((String) param.get("time")));
int i=selectService.insertSP(shangpin);
if(i>0){
model.addAttribute("info","商品上傳成功!");
return "forward:getSP";
}
model.addAttribute("info","商品上傳失敗!");
return "forward:getSP";
}
對了,千萬不能忘記實體類,就和上邊那張表的字段相對應,一個也不能錯,錯了它也會錯。
package com.qianlong.entity;
import java.util.Date;
public class Shangpin {
private Integer id;
private String name;
private String size;
private double price;
private String sellerAccount;
private int number;
private String color;
private double preprice;
private String picpath;
private Date time;
private String kind;
//set和get方法省略
service層忽略,來看dao層,也就是mapper文件
/**
* 上架商品(上傳商品圖片)
*/
@Insert(value = "insert into shangpin(name,size,price,sellerAccount,number,color," +
"preprice,picpath,time,kind) values(#{name},#{size},#{price}," +
"#{sellerAccount},#{number},#{color},#{preprice},#{picpath},#{time},#{kind})")
int insertSP(Shangpin shangpin);
然后就完成上傳了,上傳成功之后跳轉到列表頁面,使用thymeleaf提示上傳成功
<!DOCTYPE html>
<html xmlns:th="www.thymeleaf.org">
<head>
<meta charset="utf-8" />
<title>商品管理</title>
<script th:src="@{/js/jquery.1.12.4.min.js}" charset="UTF-8"></script>
</head>
<body>
<a style="margin-left: 440px" href="/demo/backAddSP">商品上架</a><span style="color: red;" th:text="${info}"></span>
<table border="4">
<thead>
<tr>
<th width="100px">序號</th>
<th width="100px">商品名稱</th>
<th width="100px">尺寸</th>
<th width="100px">價格</th>
<th width="100px">數量</th>
<th width="100px">顏色</th>
<th width="100px">原價</th>
<th width="100px">商品類型</th>
<th width="100px">圖片路徑</th>
<th width="100px">添加時間</th>
<th width="100px">操作</th>
</tr>
</thead>
<tbody>
<!-- 遍歷集合,如果被遍歷的變量 userList 為 null 或者不存在,則不會進行遍歷,也不報錯-->
<tr th:each="user : ${list}">
<!-- 將用戶的主鍵 uId 存在在 name 屬性中-->
<td width="100px" th:text="${user.id}"></td>
<td width="100px" th:text="${user.name}"></td>
<td width="100px" th:text="${user.size}"></td>
<td width="100px" th:text="${user.price}"></td>
<td width="100px" th:text="${user.number}"></td>
<td width="100px" th:text="${user.color}"></td>
<td width="100px" th:text="${user.preprice}"></td>
<td width="100px" th:text="${user.kind}"></td>
<td width="100px" th:text="${user.picpath}"></td>
<!-- 使用dates對象格式化日期-->
<td width="100px" th:text="${#dates.format(user.time, 'yyyy-MM-dd')}"></td>
<td style="text-align: center" width="100px"><intput type="button" th:onclick="del([[${user.id}]]);">刪除</intput></td>
</tr>
</tbody>
</table>
<script>
function del(id) {
alert(id);
}
</script>
</body>
</html>
圖片上傳到這里就結束啦,有問題歡迎留言!
項目地址:鏈接:https://pan.baidu.com/s/1DRr1Y9h0T7nvOfEL0oGvyw 提取碼:w6dt