java ssh框架中怎么上传图片?
5个回答
展开全部
package com.lilian.framework.servlet;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.lilian.business.common.model.CmResource;
import com.lilian.framework.utils.FileLoadUtil;
/**
* 通用上传组件(可以使用uploadify等上传组件上传资源)
* @author Ares
*/
public class FileUploadServlet extends HttpServlet {
private static final long serialVersionUID = -7933946015372885027L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doProcess(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doProcess(request, response);
}
public void doProcess(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=utf-8");
// 保存后的文件名
String bornName = ""; //源文件名
String fileName = ""; //文件名
String fileFormat = ""; //文件格式
short fileType = 1; //文件类型
Long fileSize = 0l; //文件大小
String hostAddr = ""; //主机地址(主机IP/域名)
String virtualAddr = ""; //虚拟地址(相对路径)
String urlPath = ""; //URL地址(访问路径)
// 通过时间戳散列目录存储
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM/ddHH/");
Date curDate = new Date(System.currentTimeMillis()); // 获取当前时间
String fileFolder = formatter.format(curDate);
hostAddr = FileLoadUtil.getKeyValueByKeyName("file.upload.hostadd");
// virtualAddr = FileLoadUtil.getKeyValueByKeyName("file.upload.virtualdir.image") + fileFolder;
String uploadType = request.getParameter("uploadType");
if(uploadType!=null && !uploadType.equals("")){
if(uploadType.equals("1")){
virtualAddr = FileLoadUtil.getKeyValueByKeyName("file.upload.virtualdir.image") + fileFolder;
}else if(uploadType.equals("2")){
virtualAddr = FileLoadUtil.getKeyValueByKeyName("file.upload.virtualdir.config") + fileFolder;
}else{
response.getWriter().print("文件上传失败,上传类型不存在!");
return;
}
}else{
response.getWriter().print("文件上传失败,没有上传类型!");
return;
}
// urlPath = SystemLocation.getWebrootpath() + "/" + virtualAddr;
urlPath = hostAddr + virtualAddr;
// 文件存放的目录
// String savePath = FileUtil.getUploadFilePath();
String savePath = FileLoadUtil.getKeyValueByKeyName("file.upload.path") + virtualAddr;
// 这里还可以添加有业务规则的文件目录,比如允许每个用户有自己的上传文件目录
File tempDirPath = new File(savePath);
if (!tempDirPath.exists()) {
tempDirPath.mkdirs();
}
// 创建磁盘文件工厂
DiskFileItemFactory fac = new DiskFileItemFactory();
// 创建servlet文件上传组件
ServletFileUpload upload = new ServletFileUpload(fac);
// 设置charset为utf-8,上传中文文件名不会产生乱码
upload.setHeaderEncoding("UTF-8");
// 文件列表
List fileList = null;
// 解析request从而得到前台传过来的文件
try {
fileList = upload.parseRequest(request);
} catch (FileUploadException ex) {
ex.printStackTrace();
return;
}
// 遍历从前台得到的文件列表
Iterator<FileItem> it = fileList.iterator();
List<CmResource> cmResourceList = new ArrayList<CmResource>();
while (it.hasNext()) {
FileItem item = (FileItem) it.next();
if (!item.isFormField()) {
fileName = item.getName();
fileSize = item.getSize();
bornName = fileName.substring(0, fileName.lastIndexOf("."));
if (fileName == null || fileName.trim().equals("")) {
continue;
}
// 扩展名格式:
if (fileName.lastIndexOf(".") >= 0) {
fileFormat = fileName.substring(fileName.lastIndexOf(".")+1);
}
File file = null;
do {
// 生成文件名:
fileName = UUID.randomUUID().toString() + "." + fileFormat;
file = new File(savePath + fileName);
} while (file.exists());
File saveFile = new File(savePath + fileName);
try {
item.write(saveFile);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("***************************************************************");
System.out.println("bornName: " + bornName);
System.out.println("fileName: " + fileName);
System.out.println("fileFormat: " + fileFormat);
System.out.println("fileType: " + fileType);
System.out.println("fileSize: " + fileSize.longValue());
System.out.println("hostAdd: " + hostAddr);
System.out.println("virtualAddr: " + virtualAddr);
System.out.println("urlPath: " + urlPath);
System.out.println("imageURL: " + hostAddr + virtualAddr + fileName);
System.out.println("***************************************************************");
CmResource cmResource = new CmResource();
cmResource.setBornName(bornName);
cmResource.setFileName(fileName);
cmResource.setFileFormat(fileFormat);
cmResource.setFileType(fileType);
cmResource.setFileSize(fileSize);
cmResource.setHostAddr(hostAddr);
cmResource.setVirtualAddr(virtualAddr);
cmResource.setUrlPath(urlPath);
cmResourceList.add(cmResource);
}
}
// 将文件的 相对路径+源名称+文件大小 返回给response流。
request.setAttribute("cmResourceList", cmResourceList);
response.setContentType("text/html; charset=utf-8");
response.getWriter().print("文件上传成功!");
}
}
万能通用
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.lilian.business.common.model.CmResource;
import com.lilian.framework.utils.FileLoadUtil;
/**
* 通用上传组件(可以使用uploadify等上传组件上传资源)
* @author Ares
*/
public class FileUploadServlet extends HttpServlet {
private static final long serialVersionUID = -7933946015372885027L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doProcess(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doProcess(request, response);
}
public void doProcess(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=utf-8");
// 保存后的文件名
String bornName = ""; //源文件名
String fileName = ""; //文件名
String fileFormat = ""; //文件格式
short fileType = 1; //文件类型
Long fileSize = 0l; //文件大小
String hostAddr = ""; //主机地址(主机IP/域名)
String virtualAddr = ""; //虚拟地址(相对路径)
String urlPath = ""; //URL地址(访问路径)
// 通过时间戳散列目录存储
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM/ddHH/");
Date curDate = new Date(System.currentTimeMillis()); // 获取当前时间
String fileFolder = formatter.format(curDate);
hostAddr = FileLoadUtil.getKeyValueByKeyName("file.upload.hostadd");
// virtualAddr = FileLoadUtil.getKeyValueByKeyName("file.upload.virtualdir.image") + fileFolder;
String uploadType = request.getParameter("uploadType");
if(uploadType!=null && !uploadType.equals("")){
if(uploadType.equals("1")){
virtualAddr = FileLoadUtil.getKeyValueByKeyName("file.upload.virtualdir.image") + fileFolder;
}else if(uploadType.equals("2")){
virtualAddr = FileLoadUtil.getKeyValueByKeyName("file.upload.virtualdir.config") + fileFolder;
}else{
response.getWriter().print("文件上传失败,上传类型不存在!");
return;
}
}else{
response.getWriter().print("文件上传失败,没有上传类型!");
return;
}
// urlPath = SystemLocation.getWebrootpath() + "/" + virtualAddr;
urlPath = hostAddr + virtualAddr;
// 文件存放的目录
// String savePath = FileUtil.getUploadFilePath();
String savePath = FileLoadUtil.getKeyValueByKeyName("file.upload.path") + virtualAddr;
// 这里还可以添加有业务规则的文件目录,比如允许每个用户有自己的上传文件目录
File tempDirPath = new File(savePath);
if (!tempDirPath.exists()) {
tempDirPath.mkdirs();
}
// 创建磁盘文件工厂
DiskFileItemFactory fac = new DiskFileItemFactory();
// 创建servlet文件上传组件
ServletFileUpload upload = new ServletFileUpload(fac);
// 设置charset为utf-8,上传中文文件名不会产生乱码
upload.setHeaderEncoding("UTF-8");
// 文件列表
List fileList = null;
// 解析request从而得到前台传过来的文件
try {
fileList = upload.parseRequest(request);
} catch (FileUploadException ex) {
ex.printStackTrace();
return;
}
// 遍历从前台得到的文件列表
Iterator<FileItem> it = fileList.iterator();
List<CmResource> cmResourceList = new ArrayList<CmResource>();
while (it.hasNext()) {
FileItem item = (FileItem) it.next();
if (!item.isFormField()) {
fileName = item.getName();
fileSize = item.getSize();
bornName = fileName.substring(0, fileName.lastIndexOf("."));
if (fileName == null || fileName.trim().equals("")) {
continue;
}
// 扩展名格式:
if (fileName.lastIndexOf(".") >= 0) {
fileFormat = fileName.substring(fileName.lastIndexOf(".")+1);
}
File file = null;
do {
// 生成文件名:
fileName = UUID.randomUUID().toString() + "." + fileFormat;
file = new File(savePath + fileName);
} while (file.exists());
File saveFile = new File(savePath + fileName);
try {
item.write(saveFile);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("***************************************************************");
System.out.println("bornName: " + bornName);
System.out.println("fileName: " + fileName);
System.out.println("fileFormat: " + fileFormat);
System.out.println("fileType: " + fileType);
System.out.println("fileSize: " + fileSize.longValue());
System.out.println("hostAdd: " + hostAddr);
System.out.println("virtualAddr: " + virtualAddr);
System.out.println("urlPath: " + urlPath);
System.out.println("imageURL: " + hostAddr + virtualAddr + fileName);
System.out.println("***************************************************************");
CmResource cmResource = new CmResource();
cmResource.setBornName(bornName);
cmResource.setFileName(fileName);
cmResource.setFileFormat(fileFormat);
cmResource.setFileType(fileType);
cmResource.setFileSize(fileSize);
cmResource.setHostAddr(hostAddr);
cmResource.setVirtualAddr(virtualAddr);
cmResource.setUrlPath(urlPath);
cmResourceList.add(cmResource);
}
}
// 将文件的 相对路径+源名称+文件大小 返回给response流。
request.setAttribute("cmResourceList", cmResourceList);
response.setContentType("text/html; charset=utf-8");
response.getWriter().print("文件上传成功!");
}
}
万能通用
展开全部
public class struts2UploadAction extends ActionSupport {
private File uploadFile;// 得到上传的文件
private String uploadFileContentType;// 得到文件的类型
private String uploadFileFileName;// 得到文件的名称
public String load() throws IOException {
String RealPath = ServletActionContext.getServletContext().getRealPath("/piction");
File file = new File(RealPath);
if(!file.exists()){
file.mkdirs();
}
FileUtils.copyFile(uploadFile, new File(file,uploadFileFileName));
String path=RealPath+"/"+uploadFileFileName;
ServletActionContext.getRequest().setAttribute("realpath",path);
return "success";
}
public File getUploadFile() {
return uploadFile;
}
public void setUploadFile(File uploadFile) {
this.uploadFile = uploadFile;
}
public String getUploadFileContentType() {
return uploadFileContentType;
}
public void setUploadFileContentType(String uploadFileContentType) {
this.uploadFileContentType = uploadFileContentType;
}
public String getUploadFileFileName() {
return uploadFileFileName;
}
public void setUploadFileFileName(String uploadFileFileName) {
this.uploadFileFileName = uploadFileFileName;
}
}
private File uploadFile;// 得到上传的文件
private String uploadFileContentType;// 得到文件的类型
private String uploadFileFileName;// 得到文件的名称
public String load() throws IOException {
String RealPath = ServletActionContext.getServletContext().getRealPath("/piction");
File file = new File(RealPath);
if(!file.exists()){
file.mkdirs();
}
FileUtils.copyFile(uploadFile, new File(file,uploadFileFileName));
String path=RealPath+"/"+uploadFileFileName;
ServletActionContext.getRequest().setAttribute("realpath",path);
return "success";
}
public File getUploadFile() {
return uploadFile;
}
public void setUploadFile(File uploadFile) {
this.uploadFile = uploadFile;
}
public String getUploadFileContentType() {
return uploadFileContentType;
}
public void setUploadFileContentType(String uploadFileContentType) {
this.uploadFileContentType = uploadFileContentType;
}
public String getUploadFileFileName() {
return uploadFileFileName;
}
public void setUploadFileFileName(String uploadFileFileName) {
this.uploadFileFileName = uploadFileFileName;
}
}
更多追问追答
追问
谢谢!是学编程这一行业的吗?
追答
恩
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
页面使用<input id="" name="" type="file"/>标签,
一次只上传一个图片可使用ajaxfileupload插件
一次可上传多个图片可使用uploadify插件
为js插件,网上均有下载,具体使用方法可自行搜索。
后台使用File类进行处理,网上代码很多。
一次只上传一个图片可使用ajaxfileupload插件
一次可上传多个图片可使用uploadify插件
为js插件,网上均有下载,具体使用方法可自行搜索。
后台使用File类进行处理,网上代码很多。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
上传图片和上传文件是一样的道理,上传图片的 区别就是先预览图片再上传,还是先上传再预览,个人感觉还是前者要好一些,但做起来相对复杂一些。
struts中有上传下载支持的很好,你可以可以找一些上传文件的例子模仿即可。
先预览再上传的可以参照这个博客:http://blog.csdn.net/luobaolin2008/article/details/7697901
struts中有上传下载支持的很好,你可以可以找一些上传文件的例子模仿即可。
先预览再上传的可以参照这个博客:http://blog.csdn.net/luobaolin2008/article/details/7697901
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以用struts2自带的 或者自己写servlet 请求的时候请求这个servlet
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询