java上传文件到 linux 下的 tomcat 文件服务器

linux 安装 tomcat 服务器,我安装的是 tomcat 7,未测试其他版本
linux 安装 tomcat 连接 https://mp.csdn.net/mdeditor/83987640#
本次使用的阿里云 linux os7 , tomcat 7 + ssm

1、修改目录 tomcat 下的 conf 下的 web.xml 为非只读

        <init-param>
            <param-name>readonly</param-name>
            <param-value>false</param-value>
        </init-param>

vi 不太熟练,直接使用 WinScp 远程工程修改了
在这里插入图片描述

2、maven依赖添加,我之前使用tomcat9 和springboot 内置tomcat无法创建Jersey客户端,大家注意依赖

<!-- 文件上传到外网 tomcat依赖 -->
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-client</artifactId>
    <version>1.2</version>
</dependency>

<!--tomcat7以上 无法创建Jersey客户端依赖 -->
<dependency>
    <groupId>org.glassfish.jersey.bundles</groupId>
    <artifactId>jaxrs-ri</artifactId>
    <version>2.4</version>
</dependency>

<!-- mvc 文件上传依赖 -->
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>

<!-- json 依赖-->
<dependency>
    <groupId>net.sf.json-lib</groupId>
    <artifactId>json-lib</artifactId>
    <version>2.4</version>
    <type>jar</type>
    <classifier>jdk15</classifier>
    <scope>compile</scope>
</dependency>

3、MVC 文件上传配置,没配置的添加一下,不然controller 接收不到文件

 <!-- 文件上传  单位字节-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="1024000" />
        <property name="maxInMemorySize" value="4096" />
        <property name="defaultEncoding" value="UTF-8"></property>
    </bean>

4、controller 文件上传方法

   //===================文件上传到 linux下tomcat服务器======================

    @RequestMapping("/fileupload")
    public void uploadPic(HttpServletRequest request, PrintWriter out, String lastRealPath) throws IOException {
        //强制转换request
        MultipartHttpServletRequest mr = (MultipartHttpServletRequest) request;
        //从表单获得文件
        //获得文件类型input的name
        Iterator<String> iter = mr.getFileNames();
        String inputName = iter.next();
        //获得文件
        MultipartFile mf = mr.getFile(inputName);
        byte[] mfs = mf.getBytes();
        //定义上传后的文件名字
        String fileName = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        Random random = new Random();
        for(int i = 0; i < 3; i++){
            fileName = fileName + random.nextInt(10);
        }
        //获得后缀名
        String oriFileName = mf.getOriginalFilename();
        String suffix = oriFileName.substring(oriFileName.lastIndexOf("."));
        //要上传文件的绝对路径
        String realPath ="http://47.107.128.84:8080/" +"file/test/"+fileName + suffix;
        //相对路径,回写页面
        String relativePath = "file/test/"+fileName + suffix;

        //由于我们需要在不同主机上上传不能直接通过流的方式写文件
        //创建Jersey客户端,tomcat7 支持,tomcat9 需添加依赖
        Client client = Client.create();
        //判断是不是第一次上传
        if(StringUtils.isNotBlank(lastRealPath)){
            WebResource wr = client.resource(lastRealPath);
            wr.delete();
        }
        //指定要上传的具体的地址,参数就是要上传的图片的绝对路径
        WebResource wr = client.resource(realPath);
        //文件的上传到文件主机上
        wr.put(mfs);

        //使用json对象把绝对路径和相对路径写回去
        JSONObject jo = new JSONObject();
        jo.accumulate("realPath", realPath);
        jo.accumulate("relativePath", relativePath);
        //{"realPath":"http://...", "relativePath":"/upload/.."}
        String result = jo.toString();
        out.write(result);
    }

  

外加、富文本文件上传方法

  //=================富文本文件上传到 linux下tomcat服务器======================

    @RequestMapping("/uploadForFck.do")
    public void uploadForFck(HttpServletRequest request, PrintWriter out) throws IOException{
        //强制转换request
        MultipartHttpServletRequest mr = (MultipartHttpServletRequest) request;
        //从表单获得文件
        //获得文件类型input的name
        Iterator<String> iter = mr.getFileNames();
        String inputName = iter.next();
        //获得文件
        MultipartFile mf = mr.getFile(inputName);
        byte[] mfs = mf.getBytes();
        //定义上传后的文件名字
        String fileName = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        Random random = new Random();
        for(int i = 0; i < 3; i++){
            fileName = fileName + random.nextInt(10);
        }
        //获得后缀名
        String oriFileName = mf.getOriginalFilename();
        String suffix = oriFileName.substring(oriFileName.lastIndexOf("."));
        //要上传文件的绝对路径
        String realPath ="http://47.107.128.84:8080/" +"file/test/"+fileName + suffix;
        //相对路径,回写页面
        String relativePath = "file/test/"+fileName + suffix;

        //由于我们需要在不同主机上上传不能直接通过流的方式写文件
        //创建Jersey客户端
        Client client = Client.create();

        //指定要上传的具体的地址,参数就是要上传的图片的绝对路径
        WebResource wr = client.resource(realPath);
        //文件的上传到文件主机上
        wr.put(mfs);
    }

前台from


<form action="file/fileupload" method="post" enctype="multipart/form-data">
    <label>文件上传</label>
    <input type="file" name="file">
    <input type="submit" value="提交">
</form>

winscp查看上传结果,文件以已经上传上去了在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值