- 浏览: 372044 次
- 性别:
- 来自: 深圳
最新评论
-
qw575408794:
请问 直角平面坐标 转 经纬度 怎么转 java实现,或者 ...
关于经纬度坐标转换的方法 -
horsely:
JavaScript图表FusionCharts免费在线公开课 ...
FusionCharts在Java中的基本使用 -
rzh0001:
good job
Excel 日期格式与数字格式转换的BUG -
springdata-jpa:
java quartz定时任务demo教程源代码下载,地址:h ...
[JAVA]定时任务之-Quartz使用篇 -
liuweihug:
fusioncharts 图片2种方式使用java导出 - 项 ...
FusionCharts在Java中的基本使用
FileAction
package com.action;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import com.actionForm.FileActionForm;
import org.apache.struts.actions.DispatchAction;
import java.util.Date;
import java.text.*;
import org.apache.struts.upload.FormFile;
import java.io.*;
import java.net.URLEncoder;
import com.dao.*;
public class FileAction extends DispatchAction {
private JDBConnection connection =new JDBConnection();
//以下方法实现文件的上传
public ActionForward upLoadFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws
Exception {
ActionForward forward=null;
Date date = new Date();
FileActionForm fileActionForm = (FileActionForm) form;
//FormFile用于指定存取文件的类型
FormFile file = fileActionForm.getFile(); //获取当前的文件
// 获得系统的绝对路径 String dir = servlet.getServletContext().getRealPath("/image");
//我上传的文件没有放在服务器上。而是存在D:D:\\loadfile\\temp\\
String dir="D:\\loadfile\\temp\\";
int i = 0;
String type = file.getFileName();
while(i!=-1){
//找到上传文件的类型的位置,这个地方的是'.'
i = type.indexOf(".");
/* System.out.println(i);*/
/*截取上传文件的后缀名,此时得到了文件的类型*/
type = type.substring(i+1);
}
// 限制上传类型为jpg,txt,rar;
if (!type.equals("jpg") && !type.equals("txt")&& !type.equals("bmp"))
{//当上传的类型不为上述类型时,跳转到错误页面。
forward=mapping.findForward("error");
}
else
{
// 将上传时间加入文件名(这个地方的是毫秒数)
String times = String.valueOf(date.getTime());
//组合成 time.type
String fname = times + "." + type;
//InInputStream是用以从特定的资源读取字节的方法。
InputStream streamIn = file.getInputStream(); //创建读取用户上传文件的对象
//得到是字节数,即byte,我们可以直接用file.getFileSize(),也可以在创建读取对象时用streamIn.available();
// int ok=streamIn.available();
int ok=file.getFileSize();
String strFee = null;
//这个地方是处理上传的为M单位计算时,下一个是以kb,在下一个是byte;
if(ok>=1024*1024)
{
float ok1=(((float)ok)/1024f/1024f);
DecimalFormat myformat1 = new DecimalFormat("0.00");
strFee = myformat1.format(ok1)+"M";
System.out.println(strFee+"M");
}
else if(ok>1024 && ok<=1024*1024)
{
double ok2=((double)ok)/1024;
DecimalFormat myformat2=new DecimalFormat("0.00");
strFee = myformat2.format(ok2)+"kb";
System.out.println(strFee+"kb");
}
else if(ok<1024)
{
System.out.println("aaaaaaaaa");
strFee=String.valueOf(ok)+"byte";
System.out.println(strFee);
}
System.out.println( streamIn.available()+"文件大小byte");
//这个是io包下的上传文件类
File uploadFile = new File(dir); //指定上传文件的位置
if (!uploadFile.exists() || uploadFile == null) { //判断指定路径dir是否存在,不存在则创建路径
uploadFile.mkdirs();
}
//上传的路径+文件名
String path = uploadFile.getPath() + "\\" + fname;
//OutputStream用于向某个目标写入字节的抽象类,这个地方写入目标是path,通过输出流FileOutputStream去写
OutputStream streamOut = new FileOutputStream(path);
int bytesRead = 0;
byte[] buffer = new byte[8192];
//将数据读入byte数组的一部分,其中读入字节数的最大值是8192,读入的字节将存储到,buffer[0]到buffer[0+8190-1]的部分中
//streamIn.read方法返回的是实际读取字节数目.如果读到末尾则返回-1.如果bytesRead返回为0则表示没有读取任何字节。
while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
//写入buffer数组的一部分,从buf[0]开始写入并写入bytesRead个字节,这个write方法将发生阻塞直至字节写入完成。
streamOut.write(buffer, 0, bytesRead);
}
// 关闭输出输入流,销毁File流。
streamOut.close();
streamIn.close();
file.destroy();
String paths=path;
System.out.println(paths);
String fileName = Chinese.toChinese(fileActionForm.getFileName()); //获取文件的名称
//String fileSize = String.valueOf(file.getFileSize());
String fileDate = DateFormat.getDateInstance().format(date);
String sql = "insert into tb_file values('" + fileName + "','" +
strFee + "','" + fileDate + "','" + paths + "')";
connection.executeUpdate(sql);
connection.closeConnection();
forward=mapping.findForward("upLoadFileResult");
}
return forward;
}
//实现文件的下载
public ActionForward downFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws
Exception {
String path = request.getParameter("path");
System.out.println(path+"111");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
//如果是从服务器上取就用这个获得系统的绝对路径方法。 String filepath = servlet.getServletContext().getRealPath("/" + path);
String filepath=path;
System.out.println("文件路径"+filepath);
File uploadFile = new File(filepath);
fis = new FileInputStream(uploadFile);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
//这个就就是弹出下载对话框的关键代码
response.setHeader("Content-disposition",
"attachment;filename=" +
URLEncoder.encode(path, "utf-8"));
int bytesRead = 0;
//这个地方的同上传的一样。我就不多说了,都是用输入流进行先读,然后用输出流去写,唯一不同的是我用的是缓冲输入输出流
byte[] buffer = new byte[8192];
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush();
fis.close();
bis.close();
fos.close();
bos.close();
return null;
}
}
FileActionForm
package com.actionForm;
import org.apache.struts.action.*;
import org.apache.struts.upload.*;
public class FileActionForm extends ActionForm {
private String fileName;//上传文件的名称
private String fileSize;//上传文件的大小
private String filePath;//上传文件到服务器的路径
private String fileDate;//上传文件的日期
private FormFile file;//上传文件
public String getFileName() {
return fileName;
}
public FormFile getFile() {
return file;
}
public String getFileSize() {
return fileSize;
}
public String getFilePath() {
return filePath;
}
public String getFileDate() {
return fileDate;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public void setFile(FormFile file) {
this.file = file;
}
public void setFileSize(String fileSize) {
this.fileSize = fileSize;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public void setFileDate(String fileDate) {
this.fileDate = fileDate;
}
}
index.jsp
<table width="264" height="81" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="115" rowspan="4" align="center"><img src="<%=form.getFilePath()%>" width="100" height="100"></td>
<td width="133" align="center">图片名称:<%=form.getFileName()%></td>
</tr>
<tr align="center">
<td>图片大小:<%=form.getFileSize()%></td>
</tr>
<tr align="center">
<td>上传日期:<%=form.getFileDate()%></td>
</tr>
<tr>
<td align="center"><a href="fileAction.do?method=downFile&path=<%=form.getFilePath()%>" ><img src="priture/bottond.jpg"></a>
</td>
</tr>
</table>
<html:form action="fileAction.do?method=upLoadFile" enctype="multipart/form-data" onsubmit="return Mycheck()">
<table height="52" border="0" align="center" cellpadding="0" cellspacing="0">
<tr align="center">
<td width="60" height="26">图片名称:</td>
<td width="160"> <html:text property="fileName"/> </td>
<td width="60">图片路径:</td>
<td width="198"> <html:file property="file"/> </td>
</tr>
<tr align="right">
<td height="26" colspan="4"> <html:submit>上传</html:submit> </td>
</tr>
</table>
</html:form>
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="fileActionForm" type="com.actionForm.FileActionForm" />
</form-beans>
<action-mappings>
<action name="fileActionForm" parameter="method" path="/fileAction" scope="request" type="com.action.FileAction" validate="true">
<forward name="upLoadFileResult" path="/result.jsp"/>
<forward name="error" path="/fail.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="ApplicationResources" />
</struts-config>
package com.action;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import com.actionForm.FileActionForm;
import org.apache.struts.actions.DispatchAction;
import java.util.Date;
import java.text.*;
import org.apache.struts.upload.FormFile;
import java.io.*;
import java.net.URLEncoder;
import com.dao.*;
public class FileAction extends DispatchAction {
private JDBConnection connection =new JDBConnection();
//以下方法实现文件的上传
public ActionForward upLoadFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws
Exception {
ActionForward forward=null;
Date date = new Date();
FileActionForm fileActionForm = (FileActionForm) form;
//FormFile用于指定存取文件的类型
FormFile file = fileActionForm.getFile(); //获取当前的文件
// 获得系统的绝对路径 String dir = servlet.getServletContext().getRealPath("/image");
//我上传的文件没有放在服务器上。而是存在D:D:\\loadfile\\temp\\
String dir="D:\\loadfile\\temp\\";
int i = 0;
String type = file.getFileName();
while(i!=-1){
//找到上传文件的类型的位置,这个地方的是'.'
i = type.indexOf(".");
/* System.out.println(i);*/
/*截取上传文件的后缀名,此时得到了文件的类型*/
type = type.substring(i+1);
}
// 限制上传类型为jpg,txt,rar;
if (!type.equals("jpg") && !type.equals("txt")&& !type.equals("bmp"))
{//当上传的类型不为上述类型时,跳转到错误页面。
forward=mapping.findForward("error");
}
else
{
// 将上传时间加入文件名(这个地方的是毫秒数)
String times = String.valueOf(date.getTime());
//组合成 time.type
String fname = times + "." + type;
//InInputStream是用以从特定的资源读取字节的方法。
InputStream streamIn = file.getInputStream(); //创建读取用户上传文件的对象
//得到是字节数,即byte,我们可以直接用file.getFileSize(),也可以在创建读取对象时用streamIn.available();
// int ok=streamIn.available();
int ok=file.getFileSize();
String strFee = null;
//这个地方是处理上传的为M单位计算时,下一个是以kb,在下一个是byte;
if(ok>=1024*1024)
{
float ok1=(((float)ok)/1024f/1024f);
DecimalFormat myformat1 = new DecimalFormat("0.00");
strFee = myformat1.format(ok1)+"M";
System.out.println(strFee+"M");
}
else if(ok>1024 && ok<=1024*1024)
{
double ok2=((double)ok)/1024;
DecimalFormat myformat2=new DecimalFormat("0.00");
strFee = myformat2.format(ok2)+"kb";
System.out.println(strFee+"kb");
}
else if(ok<1024)
{
System.out.println("aaaaaaaaa");
strFee=String.valueOf(ok)+"byte";
System.out.println(strFee);
}
System.out.println( streamIn.available()+"文件大小byte");
//这个是io包下的上传文件类
File uploadFile = new File(dir); //指定上传文件的位置
if (!uploadFile.exists() || uploadFile == null) { //判断指定路径dir是否存在,不存在则创建路径
uploadFile.mkdirs();
}
//上传的路径+文件名
String path = uploadFile.getPath() + "\\" + fname;
//OutputStream用于向某个目标写入字节的抽象类,这个地方写入目标是path,通过输出流FileOutputStream去写
OutputStream streamOut = new FileOutputStream(path);
int bytesRead = 0;
byte[] buffer = new byte[8192];
//将数据读入byte数组的一部分,其中读入字节数的最大值是8192,读入的字节将存储到,buffer[0]到buffer[0+8190-1]的部分中
//streamIn.read方法返回的是实际读取字节数目.如果读到末尾则返回-1.如果bytesRead返回为0则表示没有读取任何字节。
while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
//写入buffer数组的一部分,从buf[0]开始写入并写入bytesRead个字节,这个write方法将发生阻塞直至字节写入完成。
streamOut.write(buffer, 0, bytesRead);
}
// 关闭输出输入流,销毁File流。
streamOut.close();
streamIn.close();
file.destroy();
String paths=path;
System.out.println(paths);
String fileName = Chinese.toChinese(fileActionForm.getFileName()); //获取文件的名称
//String fileSize = String.valueOf(file.getFileSize());
String fileDate = DateFormat.getDateInstance().format(date);
String sql = "insert into tb_file values('" + fileName + "','" +
strFee + "','" + fileDate + "','" + paths + "')";
connection.executeUpdate(sql);
connection.closeConnection();
forward=mapping.findForward("upLoadFileResult");
}
return forward;
}
//实现文件的下载
public ActionForward downFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws
Exception {
String path = request.getParameter("path");
System.out.println(path+"111");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
//如果是从服务器上取就用这个获得系统的绝对路径方法。 String filepath = servlet.getServletContext().getRealPath("/" + path);
String filepath=path;
System.out.println("文件路径"+filepath);
File uploadFile = new File(filepath);
fis = new FileInputStream(uploadFile);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
//这个就就是弹出下载对话框的关键代码
response.setHeader("Content-disposition",
"attachment;filename=" +
URLEncoder.encode(path, "utf-8"));
int bytesRead = 0;
//这个地方的同上传的一样。我就不多说了,都是用输入流进行先读,然后用输出流去写,唯一不同的是我用的是缓冲输入输出流
byte[] buffer = new byte[8192];
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush();
fis.close();
bis.close();
fos.close();
bos.close();
return null;
}
}
FileActionForm
package com.actionForm;
import org.apache.struts.action.*;
import org.apache.struts.upload.*;
public class FileActionForm extends ActionForm {
private String fileName;//上传文件的名称
private String fileSize;//上传文件的大小
private String filePath;//上传文件到服务器的路径
private String fileDate;//上传文件的日期
private FormFile file;//上传文件
public String getFileName() {
return fileName;
}
public FormFile getFile() {
return file;
}
public String getFileSize() {
return fileSize;
}
public String getFilePath() {
return filePath;
}
public String getFileDate() {
return fileDate;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public void setFile(FormFile file) {
this.file = file;
}
public void setFileSize(String fileSize) {
this.fileSize = fileSize;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public void setFileDate(String fileDate) {
this.fileDate = fileDate;
}
}
index.jsp
<table width="264" height="81" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="115" rowspan="4" align="center"><img src="<%=form.getFilePath()%>" width="100" height="100"></td>
<td width="133" align="center">图片名称:<%=form.getFileName()%></td>
</tr>
<tr align="center">
<td>图片大小:<%=form.getFileSize()%></td>
</tr>
<tr align="center">
<td>上传日期:<%=form.getFileDate()%></td>
</tr>
<tr>
<td align="center"><a href="fileAction.do?method=downFile&path=<%=form.getFilePath()%>" ><img src="priture/bottond.jpg"></a>
</td>
</tr>
</table>
<html:form action="fileAction.do?method=upLoadFile" enctype="multipart/form-data" onsubmit="return Mycheck()">
<table height="52" border="0" align="center" cellpadding="0" cellspacing="0">
<tr align="center">
<td width="60" height="26">图片名称:</td>
<td width="160"> <html:text property="fileName"/> </td>
<td width="60">图片路径:</td>
<td width="198"> <html:file property="file"/> </td>
</tr>
<tr align="right">
<td height="26" colspan="4"> <html:submit>上传</html:submit> </td>
</tr>
</table>
</html:form>
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="fileActionForm" type="com.actionForm.FileActionForm" />
</form-beans>
<action-mappings>
<action name="fileActionForm" parameter="method" path="/fileAction" scope="request" type="com.action.FileAction" validate="true">
<forward name="upLoadFileResult" path="/result.jsp"/>
<forward name="error" path="/fail.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="ApplicationResources" />
</struts-config>
发表评论
-
cpu架构为power的JDk部署 jdk7 ppc64
2015-01-29 11:49 1946IBM提供了在cpu架构为power的linux操作系统的J ... -
CentOS 6.3下Samba服务器的安装与配置
2015-01-04 11:11 2538最近公司存储服务器 ... -
Intellij IDEA 快捷键整理
2014-10-21 15:54 854【常规】 Ctrl+Shift + Enter,语句完成 ... -
Maven: javax.sql:jdbc-stdext:2.0
2014-10-21 15:05 902今天搞maven遇到了这个小问题,google下,也没合适的 ... -
MySQL监控、性能分析——工具篇
2014-10-15 14:36 4895MySQL越来越被更多企业接受,随着企业发展,MySQL存储 ... -
Tomcat启动分析 【转】
2014-10-13 16:07 8831 - Tomcat Server的组成 ... -
MySQL Migration Toolkit initialized java loader 出错提示jre版本问题
2014-09-29 13:55 1779如果initialized java loader 出错提示j ... -
MySQL Migration Toolkit的使用
2014-09-29 13:54 1112MySQL Migration Toolkit是MySQL出 ... -
Oracle to MySQL
2014-09-29 13:52 10181.java 实体的移植 主键生成策略有JPA 提供与 ... -
jacob常用异常处理
2014-09-17 16:53 3258com.jacob.com.ComFailException ... -
java 使用jacob 操作word
2014-09-17 16:39 2348/** * word文档 */ ... -
HttpCLient实现对被GZip压缩过的Response进行解压
2014-05-22 15:33 11978发送请求(要求服务端对response进行GZip压缩): ... -
关于HttpClient的总结
2014-05-22 14:28 1207关于Httpclient的使用总结如下: (1)当Htt ... -
设置Tomcat的JAVA_OPTS参数
2014-03-20 17:23 1054修改 TOMCAT/bin/catalina.bat添加se ... -
Java_Thumbnailator
2014-03-20 14:49 708Thumbnailator 是一个为Java界面更流畅的缩略图 ... -
maven添加oracle驱动
2013-10-09 16:44 1224由于oracle商业版权问题,maven是不可以直接下载ja ... -
直接使用SQL操作Oracle空间数据的原理以及配置方法
2013-07-10 16:43 1718最近一直接到售前的请求,客户现场成功部署SDE for Or ... -
ORA-22992: 无法使用从远程表选择的 LOB 定位器
2013-07-09 11:08 1039现象描述:执行一条语句时报错,该语句是:select * ... -
ORA-28575:无法打开与外部过程代理程序的RPC连接
2013-07-09 10:54 18071. 修改listener.ora文件,增加如下内容,注意该 ... -
DBMS_STATS分析表
2013-07-05 16:54 906作用:DBMS_STATS.GATHER_TABLE_ST ...
相关推荐
struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载
Struts1的fileupload的文件上传
Struts文件的上传和下载Struts文件的上传和下载Struts文件的上传和下载
使用struts1实现文件上传和下载,上传有单文件上传,多文件上传,动态添加上传文件域。
struts1实现文件上传和下载,struts1实现文件上传和下载。
在struts2基础上实现的包括单文件、多文件的上传及下载
struts1和struts2分别实现文件上传下载功能
struts实现的文件上传下载功能,最大可以上传到150MB
struts 单文件上传和多文件上传带进度条
用struts实现文件的上传和下载,在实现中,对于文件大小大于3M的进行限定.(注意版本问题)
struts2的文件上传和文件下载的代码,出品必属精品
struts2中的文件上传和下载示例 struts2中的文件上传和下载示例 struts2中的文件上传和下载示例
用struts1实现文件的上传和下载,在本机已测试成功,一个简单的实现文件上传和下载的功能
struts2 文件上传和下载struts2 文件上传和下载
Struts2中文件上传与下载案例
Struts2 文件上传下载 多文件 单文件 实例;支持单个或多个文件的上传下载,并且自定义上传文件的大小和类型!!!
Struts2.0 文件上传与下载全解析Struts2.0 文件上传与下载全解析
struts2文件上传与下载,eclipse与myeclipse导入即可使用的代码,简单易懂,希望对你有帮助,喜欢的给个五星评价,谢谢!!!
struts1 实现文件上传下载...
Struts框架文件上传下载案例,需要的自取。