在Ideal环境下创建一个SpringBoot项目,客户端文件上传、下载和二维码生成的功能。
(1)在下列主界面中通过点击超链接分别进入进入文件上传页面、文件下载页面和二维码生成页面。
(2)在下列文件上传页面中,选择要上传的文件,点击提交按钮,则将选择的文件上传到服务器指定的目录下。
(3)在下列文件下载页面中,点击下列超链接,则将超链接的文件下载到客户端指定的目录下。
(4)在下列二维码生成页面中,点击“生成二维码“按钮,则二维码输出到客户端。
项目:
链接:
防封,wwm.lanzn.com/insm91z9j8wf ,,
效果如下:
主页如下:
文件上传页面:
注意,我没有test目录,我修改到"E:/test":
点击提交后:
文件下载页面:
点击下载后:
二维码页面,二维码为百度:
扫描后:
项目如下:
核心代码:
MyController.java:
package spring.demo;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.InetAddress;
import java.util.List;
@Controller
public class MyController {
//获取主机地址和主机名
public String getAddress() {
try {
InetAddress add = InetAddress.getLocalHost();
return "主机地址:" + add.getHostAddress() + "主机名:" + add.getHostName();
} catch (Exception ex) {
ex.printStackTrace();
return "无法获取地址 ";
}
}
//获取主页面mainfile.htm1
@RequestMapping("/")
public String mainfile(Model model) {
model.addAttribute("backMessage", getAddress());
return "/mainfile";
}
@RequestMapping("multifile")
public String multifile(Model model){
model.addAttribute("backMessage", getAddress());
return "/multifile";
}
// 实现多文件上传
@RequestMapping("/multifileUpload")
//4.设置请求映射的参数(与客户端访问的URL地址关联)
public @ResponseBody String multifileUpload(
@RequestParam("fileName") List<MultipartFile> files,
HttpServletRequest request) {
if (files.isEmpty()) {
return "没有要上传的文件";
}
String path = "E:/test";
int i = 0;
String backMes = "";
for (MultipartFile file : files) {
i++;
String fileName = file.getOriginalFilename();
int size = (int) file.getSize();
System.out.println(fileName + "-->" + size);
if (file.isEmpty()) {
//5,将"第?个文件上传为空\n"字符串累加到backMes变量中
backMes+="第"+i+"个文件上传为空\n";
}
else {
File dest = new File(path + "/" + fileName);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdir();
}
try {
file.transferTo(dest);
backMes+="第"+i+"个文件上传成功\n";
} catch (Exception e) {
e.printStackTrace();
return "第" + i + "个文件上传失败";
}
}
}
return backMes;
}
@RequestMapping("/downfile")
public String downFile(Model model){
model.addAttribute("backMessage", getAddress());
return "/downfile";
}
@RequestMapping("/download")
public String downLoad(HttpServletResponse response,String filename){
String filePath = "E:/test";
File file = new File(filePath+"/"+filename);
if ((file.exists())){
response.setContentType("application/force-download");
response.setHeader("Content-Disposition","attachment;fileName="+filename);
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
OutputStream os = null;
try{
os = response.getOutputStream();
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
int i = bis.read(buffer);
while(i != -1){
os.write(buffer);
i = bis.read(buffer);
}
}catch (Exception e){
e.printStackTrace();
}
System.out.println("------------file download " + filename);
try {
bis.close();
fis.close();
}catch (Exception e){
e.printStackTrace();
}
}
return null;
}
@RequestMapping("/QRfile")
public String Qrfile(Model model){
model.addAttribute("backMessage", getAddress());
return "/QRfile";
}
@RequestMapping("/QRCode")
public void qrCode(HttpServletResponse response) throws WriteAbortedException, IOException, WriterException {
Qrkit.createZxing(response,100,100,20,"H","jpg","www.baidu.com");
}
}