栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

时间修改与文件上传

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

时间修改与文件上传

一、时间修改

simpledateformat:时间格式

二、文件上传:


        
        
            commons-fileupload
            commons-fileupload
            1.3.1
        
        
        
        
            com.sun.jersey
            jersey-client
            1.19
        
        
            com.sun.jersey
            jersey-core
            1.19
        
        

 工具类:

JesyFileUploadUtil.java

package com.rz.cms.server.main.utils;

import java.io.IOException;
import java.util.Date;
import java.util.Random;

import javax.net.ssl.SSLContext;


import org.springframework.web.multipart.MultipartFile;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;



 

public class JesyFileUploadUtil {
 
	
	public static FileInfo uploadFile(MultipartFile file, String serverUrl) throws IOException {
		//把request请求转换成多媒体的请求对象
//		MultipartHttpServletRequest mh = (MultipartHttpServletRequest) request;
		//根据文件名获取文件对象
//		MultipartFile cm = mh.getFile(fileName);
		//获取文件的上传流
		byte[] fbytes = file.getBytes();
		
		//重新设置文件名
		String newFileName = "";
		newFileName += new Date().getTime()+""; //将当前时间获得的毫秒数拼接到新的文件名上
		//随机生成一个3位的随机数
		Random r = new Random();
		for(int i=0; i<3; i++) {
			newFileName += r.nextInt(10); //生成一个0-10之间的随机整数
		}
		
		//获取文件的扩展名
		String orginalFilename = file.getOriginalFilename();
		String suffix = orginalFilename.substring(orginalFilename.indexOf("."));
		
		
		//创建jesy服务器,进行跨服务器上传
//		Client client = Client.create(getClientConfig());
		Client client = Client.create();
		//把文件关联到远程服务器
		//http://127.0.0.1:8080/ssm_image_server/upload/123131312321.jpg
		WebResource resource = client.resource(serverUrl+"/upload/"+newFileName+suffix);
		//上传
		resource.put(String.class, fbytes);
		
		//图片上传成功后要做的事儿
		//1、ajax回调函数做图片回显(需要图片的完整路径)
		//2、将图片的路径保存到数据库(需要图片的相对路径)
		String fullPath = serverUrl+"/upload/"+newFileName+suffix; //全路径
		String relativePath = "/upload/"+newFileName+suffix; //相对路径
		
//		//生成一个json响应给客户端
//		String resultJson = "{"fullPath":""+fullPath+"", "relativePath":""+relativePath+""}";
//		return resultJson;

		return new FileInfo(newFileName+suffix, false, fullPath, relativePath);
	}

	
}

FileInfo.java:

package com.rz.cms.server.main.utils;

public class FileInfo {

	private String fileName;
	private boolean isSecret;
	private String absolutPath;
	private String relativePath;

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getAbsolutPath() {
		return absolutPath;
	}

	public void setAbsolutPath(String absolutPath) {
		this.absolutPath = absolutPath;
	}

	public String getRelativePath() {
		return relativePath;
	}

	public void setRelativePath(String relativePath) {
		this.relativePath = relativePath;
	}

	public boolean isSecret() {
		return isSecret;
	}

	public void setSecret(boolean isSecret) {
		this.isSecret = isSecret;
	}

	public FileInfo() {
	}

	public FileInfo(String fileName, boolean isSecret, String absolutPath, String relativePath) {
		super();
		this.fileName = fileName;
		this.isSecret = isSecret;
		this.absolutPath = absolutPath;
		this.relativePath = relativePath;
	}

	@Override
	public String toString() {
		return "FileInfo [fileName=" + fileName + ", isSecret=" + isSecret + ", absolutPath=" + absolutPath
				+ ", relativePath=" + relativePath + "]";
	}

}

controller:

package com.rz.cms.server.main.controller.news;

import com.rz.cms.server.main.utils.FileInfo;
import com.rz.cms.server.main.utils.HttpResult;
import com.rz.cms.server.main.utils.JesyFileUploadUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/file")
public class FileController {
    @PostMapping("/upload")
    @ResponseBody
    @CrossOrigin(origins = "*")
    public String  upload(MultipartFile file){
        String result = null;
        try {
            FileInfo fileInfo = JesyFileUploadUtil.uploadFile(file, "http://localhost:8060");
            result = fileInfo.getRelativePath();
        } catch (Exception e) {
            e.printStackTrace();
            result = null;
        }
        return  result;
    }
}
 @GetMapping("/regedit/{id}/{title}/{descn}/{imgurl}")
    @ResponseBody
    @CrossOrigin(origins = "*")
    public HttpResult regedit(@PathVariable int id,@PathVariable String title,@PathVariable String descn,@PathVariable String imgurl){
        return articleService.regedit(id,title,descn,imgurl);
    }

或

前端页面