怎样在Win7系统中搭建Web服务器
详见百度搭建教程web服务器搭建
web服务器搭建
搭建好服务器以后配置
controller层
/**
* 上传头像
*/
@RequestMapping(value="uploadPhotoHead")
@ResponseBody
public String uploadPhotoHead(HttpServletRequest request,Integer userId){
Result<String> result=new Result<>(false);
try {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile file = multipartRequest.getFile("upFile");
result=userBizService.uploadPhotoHead(file,userId);
} catch (Exception e) {
result.setMessage(e.getMessage());
}
return result.toJsonResult();
}
UserBizServiceImpl实现类
@Override
@Transactional(readOnly = false)
public Result<String> uploadPhotoHead(MultipartFile file,Integer userId) {
Result<String> result=new Result<>(false);
User user=userService.findByPrimaryKey(userId);
if(user==null){
result.setMessage("用户不能为空");
result.setSuccess(false);
return result;
}
String date=new SimpleDateFormat("yyyyMMdd").format(new Date());
File file2=new File(file.getOriginalFilename());
try {
file.transferTo(file2);
ftpUtils.connect("", CommonConstant.addr, CommonConstant.port, CommonConstant.username, CommonConstant.password);
if(ftpUtils.isExistDir(date)){
uploadPicture( file2, date);
}else{
ftpUtils.mkDir(date);
uploadPicture( file2, date);
}
} catch (Exception e) {
throw new RuntimeException(e);
}finally {
if(file2.exists()){
file2.delete();
}
}
user.setPhotoHead("/"+date+"/"+file.getOriginalFilename());
userService.updateByPrimaryKeySelective(user);
result.setMessage("上传成功");
result.setSuccess(true);
return result;
}
private void uploadPicture( File file2, String date) throws Exception {
ftpUtils.changeWorkingDirectory(date);
ftpUtils.upload(file2);
ftpUtils.closeFtp();
}
FtpUtils 工具类
package com.baobaotao.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.stereotype.Component;
@Component
public class FtpUtils {
private FTPClient ftp;
/**
*
* @param path
* 上传到ftp服务器哪个路径下
* @param addr
* 地址
* @param port
* 端口号
* @param username
* 用户名
* @param password
* 密码
* @return
* @throws Exception
*/
public boolean connect(String path, String addr, int port, String username, String password) throws Exception {
boolean result = false;
ftp = new FTPClient();
ftp.setControlEncoding("GBK");
int reply;
ftp.connect(addr, port);
ftp.login(username, password);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
result = ftp.changeWorkingDirectory(path);
return result;
}
/**
*
* @param file
* 上传的文件或文件夹
* @throws Exception
*/
public void upload(File file) throws Exception {
if (file.isDirectory()) {
ftp.makeDirectory(file.getName());
ftp.changeWorkingDirectory(file.getName());
String[] files = file.list();
for (int i = 0; i < files.length; i++) {
File file1 = new File(file.getPath() + "\\" + files[i]);
if (file1.isDirectory()) {
upload(file1);
ftp.changeToParentDirectory();
} else {
File file2 = new File(file.getPath() + "\\" + files[i]);
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
}
} else {
try {
File file2 = new File(file.getPath());
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
/**
* 关闭连接
*/
public void closeFtp() {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 更改目录
*
* @param path
*/
public void changeWorkingDirectory(String path) {
if (ftp.isConnected()) {
try {
ftp.changeWorkingDirectory(path);
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("未连接");
}
}
/**
* 判断ftp上是否存在路径
*
* @return boolean
*/
public boolean isExistDir(String path) {
try {
return ftp.changeWorkingDirectory(path);
} catch (IOException e) {
return false;
}
}
/**
* 在ftp上创建文件夹
*
* @param dir
* 文件夹名字
*/
public void mkDir(String dir) {
try {
ftp.makeDirectory(dir);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
FtpUtils t = new FtpUtils();
t.connect("", "10.101.145.59", 2121, "ftp", "Ac549058387");
t.changeWorkingDirectory("/FtpDownload/20160913");
File file = new File("D:\\xx.txt");
t.upload(file);
t.closeFtp();
System.out.println("cc");
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>请上传用户头像</title>
</head>
<body>
<h1>
请选择上传的头像文件
</h1>
<form method="post" action="<c:url value="/baobaotao/user/uploadPhotoHead"/>" enctype="multipart/form-data">
<input type="text" name="name" value="中国"/>
<input type="file" name="upFile" />
<input name="userId" value="1">
<input type="submit" />
</form>
<img src="http://10.101.145.59:8080/20160913/Tulips.jpg"/>
</body>
</html>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)