请教各位大虾,我对上传和下载的基本原理和思想不是很清楚,还有就是关于数据库方面的。比如现在我有4个类别,每个类别分别对应相应的文件,数据库是存放文件的目录(真实路径还是。。)还是文件名称抑或是两者都存,或者还有4个类别就分别建立4个文件夹来存储用户上传的文件吗?还是用其他的方法?能不能提供一个详细的数据库表结构,让小弟参考一下,谢谢,不甚感激!
解决方案 »
- "cannot find symbol" 问题
- 推荐个jsp 弄的cms
- 关于jsp显示下拉列表框的问题
- 求助
- 请教:http://localhost:8080和http://192.168.0.5:8080有什么区别?
- 如何在javascript中实现刷新(如同在按F5),请教各位高手
- JAVA技术群!
- 为什么我保存为word文档时,对话框默认的保存文件类型是jsp?能不能指定默认类型和文件名?
- 聊天室非本地不能访问的问题啊!100可以在加分啊
- 高手请帮忙解决JavaBean的问题?(急,我给300分)
- 分享 Jquery+JAVA实现开心网上传头像剪裁功能
- java文件上传在firefox下正常,在IE下出错!
上传用FormFile类,记得下载Commons FileUpload包
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.loner.struts.action;import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;import com.loner.struts.form.FileForm;/**
* MyEclipse Struts
* Creation date: 12-08-2007
*
* XDoclet definition:
* @struts.action path="/file" name="fileForm" input="/file.jsp" scope="request" validate="true"
*/
public class FileAction extends Action {
/*
* Generated Methods
*/ /**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
FileForm fileForm = (FileForm) form;
String dir = servlet.getServletContext().getRealPath("/upload");//获得一个目录,这个目录
FormFile file = fileForm.getFile();//取得文件的值
String fname = file.getFileName();//取的文件名
String fsize = Integer.toString(file.getFileSize())+"bytes";//取得文件大小
try{
InputStream streamIn = file.getInputStream();//构建一个流对象
OutputStream streamout = new FileOutputStream(dir+"/"+fname);
int bytesRead = 0;
byte[] buffer = new byte[8192];//定义一个字节数组
while((bytesRead=streamIn.read(buffer,0,8192))!=-1){//如果还有内容就写入定义的变量中
streamout.write(buffer,0,bytesRead);//利用输出流对象输出 }
streamout.close();
streamIn.close(); }catch(Exception ex){
ex.printStackTrace();
}
return null;
}
}
FileForm/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.loner.struts.form;import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;/**
* MyEclipse Struts
* Creation date: 12-08-2007
*
* XDoclet definition:
* @struts.form name="fileForm"
*/
public class FileForm extends ActionForm {
/*
* Generated fields
*/ /** file property */
private FormFile file;//该字段的类型必须是org.apache.struts.upload.FormFile类型
private String fileName;
private String size;
/*
* Generated Methods
*/ /**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
} /**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
} public FormFile getFile() {
return file;
} public void setFile(FormFile file) {
this.file = file;
} public String getFileName() {
return fileName;
} public void setFileName(String fileName) {
this.fileName = fileName;
} public String getSize() {
return size;
} public void setSize(String size) {
this.size = size;
} /**
* Returns the file.
* @return String
*/}
html页面<%@ page language="java" contentType="text/html; charset=gbk"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%><%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>
<jsp:useBean id="fileForm" class="com.loner.struts.form.FileForm"></jsp:useBean>
<html>
<head>
<title>JSP for FileForm form</title>
</head>
<body>
<html:form action="/file" method="post" enctype="multipart/form-data">
file : <html:file property="file"/><html:errors property="file"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>你数据库里存放的是文件相对路径最好,还有比如id,文件名,上传时间,上传用户,等等,按照你需要的来吧
在你程序里新建立个文件夹,用来保存上传来的文件
用二进制的话,可以用流直接导入数据库存储下来, 读的时候就再用流导出来即可
用文件路径的话,数据库里只是存一个字符串而已,数据库对这个文件的存在并不知道。
读出时,先从数据库中读出文件的路径,然后根据这一路径读出文件即可。至于lz说的4个类 就建4个文件夹 这完全取决与程序员的选择,你可以建或者不建,
甚至你把文件直接放在C盘根目录,只要数据库中的路径保存正确,你就可以正确读出该文件
这里很清楚。
数据库结构我说下我的想法
你有4个类别..那你建个类别表...
很简单的表..typeId 主键, typeName 名称, typeFilePath 这个类型的文件夹路径, ...还有你可以加几个re注释...这样方便以后有第5,第6...类别的增加.
再建个文件表
fileId 主键,userId 用户, typeId(外键类别的type) ,fileName 文件名称,还可以存入文件大小等信息...
上传用FormFile类,需要下载Commons FileUpload包
jsp页面主要用:
<html:file property="csvFile" size="30" style="width:333px; margin-bottom:1px; "/>
Action:FormFile file = yourActionForm.getcsvFile();
String fileName = file.getFileName();
InputStream is = file.getInputStream();
File upLoadFile = Utils.uploadFile(is,filePath+fileName);
UploadFileTbl fileInfo = new UploadFileTbl();
FileTypeMap map = FileTypeMap.getDefaultFileTypeMap();
fileInfo.setMime_type(map.getContentType(upLoadFile));
fileInfo.setFile_name(fileName);
fileInfo.setFile_data(new FileInputStream(upLoadFile.toString()));
fileInfo.setReg_user(yourActionForm.getRegistUserId());
fileInfo.setFKey(FKey);
fileInfo.setPKey(PKey);
dao.Insert(fileInfo);要用到的方法:Utils.uploadFile(is,filePath+fileName)public static final File uploadFile(InputStream is,String fname) throws IOException{
File upLoadFile = new File(fname);
OutputStream os =new FileOutputStream(upLoadFile);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while((bytesRead = is.read(buffer,0,8192)) != -1){
os.write(buffer,0,bytesRead);
}
os.flush();
is.close();
os.close();
return upLoadFile;
}要用到的Bean:public class UploadFileTbl implements Serializable {
private String FKey = null;
private String PKey = null;
private String file_name = null;
private transient InputStream file_data = null;
private String mime_type = null;
private String reg_user = null ;
private String reg_date = null; 。。
}dao的Insert方法:private static final String INSERT_FILE =
"INSERT INTO FILETABLE( FKEY,PKEY,"
+ "FILE_NAME,FILE_DATA,REGIST_USER_ID,REGIST_DATE,MIME_TYPE,MODIFY_USER_ID,MODIFY_DATE) VALUES"
+ "(?,?,?,?,?,SYSDATE,?,?,SYSDATE)";
.........
PreparedStatement pstmt = null;
InputStream is = null;
.......
pstmt = con.prepareStatement(INSERT_FILE);
pstmt.setString(1, fileInfo.FKey);
pstmt.setString(2, fileInfo.PKey);
pstmt.setString(3, fileInfo.getFile_name());
is = fileInfo.getFile_data();
pstmt.setBinaryStream(4, is, is.available());
pstmt.setString(5, fileInfo.getReg_user());
pstmt.setString(6, fileInfo.getMime_type());
pstmt.setString(7, fileInfo.getReg_user());
pstmt.executeUpdate();
Action:String strPKey= request.getParameter("PKey");
UploadFileTbl file= dao.getFileByKey(strPKey);
InputStream is = file.getFile_data();
Utils.saveFile(is, response, file.getFile_name());用到的Utils.saveFile(is, response, file.getFile_name()),我这个是支持日文文件名,编码你到时候可能要改public static void saveFile(InputStream is,HttpServletResponse response,
String fileName){
try {
byte[] src = fileName.getBytes("Windows-31J");
String fname = new String(src,"ISO8859-1");
response.setContentType("text/plain; charset=Windows-31J");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition",
"attachment; filename=" + fname);
OutputStream os = response.getOutputStream();
byte buffer[] = new byte[8192];
int size;
while ((size = is.read(buffer)) != -1) {
os.write(buffer, 0, size);
}
os.flush();
is.close();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}dao的方法public UploadFileTbl getFileByKey(String strKey) throws SQLException,
ClassNotFoundException {
UploadFileTbl info = new UploadImageTbl();
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = getConnection();
pstmt = con.prepareStatement(SELECT_FILE_BY_KEY);
pstmt.setString(1, strKey);
rs = pstmt.executeQuery();
if(rs.next()){
info.setFile_name(rs.getString("FILE_NAME"));
info.setFKey(rs.getString("FKey"));
info.setReg_date(rs.getString("REGIST_DATE"));
info.setReg_user(rs.getString("REGIST_USER_ID"));
info.setPKey(rs.getString("PKey"));
info.setFile_data(rs.getBinaryStream("FILE_DATA"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if(rs != null){
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (con != null && !con.isClosed()) {
con.close();
con = null;
}
}
return info;
}jsp页面,需要先将数据查出,赋给ActionForm相应的属性:<a href="#" onClick="openImage('<bean:write name="yourActionForm" property="pkey"/>','openTempImg')">
<bean:write name="yourActionForm" property="file_name"/>
</a>