java上传图片到服务器_用JAVA代码上传图片到nginx服务器

1,安装nginx,看我之前的文章:在Linux下安装配置nginx

2,安装ftp组件,看我之前的文章:在linux中开启ftp服务

3,在usr/local/nginx/html下创建images文件夹 mkdir /usr/local/nginx/html/images

4,修改nginx.conf配置 vim /usr/local/nginx/cong/nginx.conf

b1ed63a10cd1e1bd347b8eb278d6d2f3.png

5,在home的ftp用户下创建www/images文件夹 比如我的就是test用户

mkdir /home/test/www/images

6,用ftp用户权限 chown test /home/test

chmod 777 -R /home/test

7,开启nginx ./nginx

8,测试

264274bb8ea9ee2c581cebeff3b8f59f.png

9,到这里我们的所有的准备就完成,接下来就用java代码实现上传,我就不解释了,直接上代码

jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form id="itemForm"action="/foreach/junde/saveImage.action" method="post" enctype="multipart/form-data">
	   <table>
	   <tr>
				<td>图片名称</td>
				<td>				
					<input type="text"  name="name"/> 
				</td>
			</tr>	
			<tr>
				<td>商品图片</td>
				<td>				
					<input type="file"  name="picFile"/> 
				</td>
			</tr>			
			<tr>
				<td colspan="2" align="center"><input type="submit" value="提交" />
				</td>
			</tr>
		</table>

	</form>
</body>
</html>

controller

package com.foreach.controller;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import com.foreach.pojo.Image;
import com.foreach.service.ImagesService;
import com.foreach.utils.FtpUtil;

@Controller
@RequestMapping("/junde")
public class ImagesController {
	
	@Autowired
	private ImagesService imagesService;
	
	@RequestMapping("/tiao")
	public String tiao(){
		
		return "images";
	}

	@RequestMapping(value="/saveImage",method=RequestMethod.POST)
	public String saveImage(MultipartFile picFile,String name,Model model) throws Exception{
		Image image = new Image();
		UUID uuid = UUID.randomUUID();
		String oldname = picFile.getOriginalFilename();
		String newname =uuid+ oldname.substring(oldname.lastIndexOf("."));
		boolean b = FtpUtil.uploadFile("192.168.60.128", 21, "test","test",
				"/home/test/www/images","/4/29", newname, picFile.getInputStream());
		image.setName(name);
		image.setUrl("http://192.168.60.128/images/4/29/"+newname);
		imagesService.saveImage(image);
		if(b){
			model.addAttribute("error", 1);
			model.addAttribute("url", "http://192.168.60.128/images/4/29/"+newname);
		}else{
			model.addAttribute("error",0);
			model.addAttribute("错误信息", "上传失败");
		}
		
		return "result";
	}
}

service层没有什么就是调用dao层就不帖出来了

dao层

58a3efb1fd99d2c5075cefac9980102e.png
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.foreach.mapper.ImagesMapper" >

   <insert id="saveImage"  parameterType="image">
      
      insert into image (name,url) values(#{name},#{url})
   </insert>
   
</mapper>

pojo

package com.foreach.pojo;

public class Image {
	
	private int id;
	
	private String url;
	
	private String name;

	public Image() {
		
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getName() {
		return name;
	}

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

}

10,到这里就大功告成了,就可以运行,上传图片了。我这里用了FtpUtil工具,想要的可以私信我,虽然也没人看。。。QQ2040931220

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值