服务器架构的问题-文件上传

package com.itheima.control;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.pojo.Resource;
import com.itheima.utils.JSONFileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;
@Controller
public class FileController {
    @RequestMapping("/fileUpLoad")
    public String fileUpLoad(MultipartFile[] files,
                             HttpServletRequest request) throws Exception {
        String path = request.getServletContext().getRealPath("/") + "files/";
        ObjectMapper mapper = new ObjectMapper();
        if (files != null && files.length > 0) {
            for (MultipartFile file : files) {
                String filename = files.getOriginalFilename();
                ArrayList<Resource> list = new ArrayList<Resource>();
                String json = JSONFileUtils.readFile(path + "/files.json");
                if (json.length() != 0) {
                    list = mapper.readValue(json, new TypeReference<List<Resource>>() {
                    });
                    for (Resource resource : list) {
                        if (filename.equals(resource.getName())) {
                            String[] split = filename.split("\\.");
                            filename = split[0] + "(1)." + split[1];
                        }
                    }
                }
                String filePath = path + filename;
                file.transferTo(new File(filename));
               list.add(new Resource(filename));
               json=mapper.writeValueAsString(list);
               JSONFileUtils.writeFile(json,path+"/files.json");
            }
            request.setAttribute("msg","(上传成功)");
            return "forward:fileload.jsp";
        }
        request.setAttribute("msg","(上传失败)");
        return "forward:fileload.jsp";
    }
    @ResponseBody
    @RequestMapping(value = "/getFilesName",produces = "text/html;charset=utf-8")
    public String getFilesName(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String path=request.getServletContext().getRealPath("/")+"file/files.json";
        String json=JSONFileUtils.readFile(path);
        return json;
    }
}



package com.itheima.utils;
import sun.misc.IOUtils;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class JSONFileUtils {
    public static String readFile(String filepath) throws Exception{
        FileInputStream fis=new FileInputStream(filepath);
        return IOUtils.toString(fis);
    }
    public static void writeFile(String data,String filepath)throws Exception{
        FileOutputStream fos=new FileOutputStream(filepath);
        IOUtils.write(data,fos);
    }
}

package com.itheima.pojo;

public class Resource {
    private String name;
    public Resource(String name){
        this.name=name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>实验7:文件上传和下载</title>
    <script src="${pageContext.request.contextPath}/js/jquery-3.6.0.js"
            type="text/javascript"></script>
</head>
<body>
    <table border="1">
        <tr>
            <td width="200" align="center">文件上传${msg}</td>
            <td width="300" align="center">下载列表</td>
        </tr>
        <tr>
            <td height="100">
                <form action="${pageContext.request.contextPath}/fileUpLoad"
                      method="post" enctype="multipart/form-data">
                    <input type="file" name="files" multiple="multiple"><br/>
                    <input type="reset" value="清空"/>
                    <input type="submit" value="提交"/>
                </form>
            </td>
            <td id="files"></td>
        </tr>
    </table>
</body>
<script>
    $(document).ready(function (){
        var url="${pageContext.request.contextPath}/getFilesName";
        $.get(url,function (files){
            var files=eval('('+files+')');
            for (var i=0;i<files.length;i++){
                $("#files").append("<li><a href=${pageContext.request.contextPath}"+
                "\\"+"download?filename="+files[i].name+">"+files[i].name+"</a></li>");
            }
        })
    })


</script>
</html>




<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>untitled1</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>untitled1 Maven Webapp</name>
    <url>http://maven.apache.org</url>  <dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.10.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.0</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.4</version>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>jquery</artifactId>
        <version>3.1.1</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.9.0</version>
    </dependency>
</dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <port>8083</port>
                    <path>/</path>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>4.0.0</version>
                <configuration>
                    <source>7</source>
                    <target>7</target>
                </configuration>
            </plugin>

        </plugins>
    </build></projec

img

img

img

这几个爆红的函数不是应该存在吗,有人能解答一下吗,

第一张图,
检查下你的代码,你应该是String filename = file.getOriginalFilename(),而不是String filename = files.getOriginalFilename();
第二张图,
检查下你的包是不是引错了,应该是org.apache.commons.io.IOUtils,而不是sun.misc.IOUtils
第三张图,
应该不影响运行,先不管他