BBS项目,头像存在哪个路径下?
我的做法是:
存到 D:/workspace/bbs/faces/[userid].jpg (开发时myeclipse的工作空间里)
或D:/programs/apache tomcat/webapp/bbs/faces/[userid].jpg(开发时tomcat的webapp里)
这样做有这么几个问题:
1、项目开发完之后,存储路径怎么改呢?机器变了,路径肯定不一样了
2、这个上传完后,不管是什么格式的图片都被强制命名成jpg格式的了,有什么方法可以不改变格式,这个方法是尚学堂bbs项目里的,在网上找了一些,没看懂。高分请高手提供代码!感激不尽!ps:图片根本没有往数据库里存,显示的页面直接<img src="<%=user.getId()%>".jpg/>
我的做法是:
存到 D:/workspace/bbs/faces/[userid].jpg (开发时myeclipse的工作空间里)
或D:/programs/apache tomcat/webapp/bbs/faces/[userid].jpg(开发时tomcat的webapp里)
这样做有这么几个问题:
1、项目开发完之后,存储路径怎么改呢?机器变了,路径肯定不一样了
2、这个上传完后,不管是什么格式的图片都被强制命名成jpg格式的了,有什么方法可以不改变格式,这个方法是尚学堂bbs项目里的,在网上找了一些,没看懂。高分请高手提供代码!感激不尽!ps:图片根本没有往数据库里存,显示的页面直接<img src="<%=user.getId()%>".jpg/>
解决方案 »
- logic:iterate 标签如何使用?
- •Grid报表和Free From报表产品之比较
- ExtJS的formPanel提交后,不能执行success。
- 点击新增div,里面不能有java代码么?
- 荤?郁闷几天了,高手救我啊!!sql查询问题!
- Windows下Apache与Tomcat的整合,Apache无法启动,求助
- 一般大家如何假设自己的邮件系统?
- 一张表单上有两个下拉框,第一个下拉框从数据库里取出所有的班级,第二个下拉框需要根据第一个下拉框所选择的班级而显示这个班级所有的成员
- javac命令编译一个servlet出现下面的错误,servlet-api.jar的classpath我设置好了.
- 大家发表一下意见
- 怎样将工程部署到Tomcat“根目录”下!
- Struts2多语言(国际化问题),急!急!急!
package com.xidian._3dcampus.servlet;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.*;
import java.util.*;
import java.util.regex.*;
import java.io.*;
import org.apache.commons.fileupload.servlet.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;public class BbsFileUpload extends HttpServlet { private static final long serialVersionUID = 1L; public void init(ServletConfig config) throws ServletException {
this.config = config;
} private ServletConfig config = null;
private File tempPath = new File("C:\\Documents and Settings\\"); // 用于存放临时文件的目录 public void destroy() {
config = null;
super.destroy();
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
int id = -1;
String uploadPath = config.getInitParameter("uploadPath"); // 用于存放上传文件的目录 res.setContentType("text/html; charset=GB2312");
PrintWriter out = res.getWriter(); DiskFileItemFactory factory = new DiskFileItemFactory();
// maximum size that will be stored in memory
factory.setSizeThreshold(40960);
// the location for saving data that is larger than getSizeThreshold()
factory.setRepository(tempPath); ServletFileUpload upload = new ServletFileUpload(factory);
// maximum size before a FileUploadException will be thrown
upload.setSizeMax(60000000);
try {
List fileItems = upload.parseRequest(req);
// assume we know there are two files. The first file is a small
// text file, the second is unknown and is written to a file on
// the server
Iterator iter = fileItems.iterator(); // 正则匹配,过滤路径取文件名
String regExp = ".+\\\\(.+)$"; // 过滤掉的文件类型
String[] errorType = { ".exe", ".com", ".cgi", ".jsp" };
Pattern p = Pattern.compile(regExp);
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if(item.isFormField()) {
if(item.getFieldName().equals("id")) {
id = Integer.parseInt(item.getString());
}
}
// 忽略其他不是文件域的所有表单信息
if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();
if ((name == null || name.equals("")) && size == 0)
continue;
Matcher m = p.matcher(name);
boolean result = m.find();
if (result) {
for (int temp = 0; temp < errorType.length; temp++) {
if (m.group(1).endsWith(errorType[temp])) {
throw new IOException(name + ": wrong type");
}
}
try { // 保存上传的文件到指定的目录 // 在下文中上传文件至数据库时,将对这里改写
item.write(new File(uploadPath+ id + ".jpg"));
out.print(name + " " + size + "<br>");
res.setHeader("Refresh","3;URL=../bbs/userinfo.jsp");
} catch (Exception e) {
out.println(e);
} } else {
throw new IOException("fail to upload");
}
}
}
} catch (IOException e) {
out.println(e);
} catch (FileUploadException e) {
out.println(e);
} } }
很简单的2个问题:
1.你可以把上传的图片上传到你的tomcat服务器的工程下面。不要写出绝对路径;
2.一般上传文件都有一个后缀,你那个是强制所有文件的后缀是jpg的,你可以设为默认的后缀;
也就是上传的时候的后缀。
try {
InputStream in = null ;
OutputStream out = null ;
try {
in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
byte [] buffer = new byte [BUFFER_SIZE];
while (in.read(buffer) > 0 ) {
out.write(buffer);
}
} finally {
if ( null != in) {
in.close();
}
if ( null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
//取文件后缀名
private static String getExtention(String fileName) {
System.out.println(fileName+" Filename");
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
//执行(/UploadImages 不论项目怎么移动图片始终在其项目的UploadImages文件夹内)
@Override
public String execute() {
System.out.println(contentType+" contentType");
imageFileName = new Date().getTime() + getExtention(fileName);
System.out.println(ServletActionContext.getServletContext().getRealPath( "/UploadImages"));
File imageFile = new File(ServletActionContext.getServletContext().getRealPath( "/UploadImages" ) + "/" + imageFileName);
copy(myFile, imageFile);
return SUCCESS;
}