文件上传和下载12-15讲
2009-03-20 01:02
288 查看
文件上传和下载12-15讲明天看~
对URL的字符进行解码:java.net.URLDecoder
public static String decode(String s, String enc) throws UnsupportedEncodingException
参数:
enctype="multipart/form-data" 上传文件必须设置:默认是:enctype="application/x-www-form-urlencoded"下载准备:commons-fileupload-1.2.1 和commons-io-1.4引入包得到文件的路径时候,有的浏览器是传的所有的路径,有的是传的最后的文件名。要注意,按通用的方法写。
都是自己实现的,struts2中单个的文件可以这样获取 private File file;
private String fileFileName;//file-FileName固定的
private String fileContentType;//file-ContentType固定的 org.apache.struts2.interceptor.FileUploadInterceptor
private List<String> fileFileName;
private List<String> fileContentType;存的时候循环遍历
var mo=document.getElementById("more");
var br = document.createElement("br");
var input = document.createElement("input");
var button= document.createElement("input");
input.type="file";
input.name="file";
button.type="button";
button.value="移除";
button.onclick=function(){
mo.removeChild(br);
mo.removeChild(input);
mo.removeChild(button);
}
mo.appendChild(br);
mo.appendChild(input);
mo.appendChild(button);
}看org.apache.struts2.interceptor.FileUploadInterceptor代码设置属性 protected static final Log log = LogFactory.getLog(FileUploadInterceptor.class);
private static final String DEFAULT_DELIMITER = ",";
private static final String DEFAULT_MESSAGE = "no.message.found"; protected Long maximumSize;//限制大小
protected String allowedTypes;//允许的文件类型
protected Set allowedTypesSet = Collections.EMPTY_SET;允许的文件类型参照tomcat下的conf下的web.xml如:<mime-mapping>
<extension>ppt</extension>
<mime-type>application/vnd.ms-powerpoint</mime-type>
</mime-mapping>则struts.xm:<interceptor-ref name="fileUpload">
<param name="maximumSize">20480000</param>
<param name="allowedTypes">application/vnd.ms-powerpoint</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>默认的defaultStack要设置错误的信息的key在代码中也有,用自己的信息,在message文件中替换就行了配置文件:
对URL的字符进行解码:java.net.URLDecoder
public static String decode(String s, String enc) throws UnsupportedEncodingException
参数:
s- 要解码的
String
enc- 所支持的字符编码的名称。 返回: 新解码的
String
enctype="multipart/form-data" 上传文件必须设置:默认是:enctype="application/x-www-form-urlencoded"下载准备:commons-fileupload-1.2.1 和commons-io-1.4引入包得到文件的路径时候,有的浏览器是传的所有的路径,有的是传的最后的文件名。要注意,按通用的方法写。
@SuppressWarnings("unchecked") public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //文件的工厂类org.apache.commons.fileupload.disk.DiskFileItemFactory; DiskFileItemFactory factory = new DiskFileItemFactory(); //目录设为当前根目录,我的是WebRoot factory.setRepository(new File(request.getRealPath("/"))); //设置文件的大小。超过写到本地缓存中, 默认10240 factory.setSizeThreshold(5 * 1024 * 1024);//5M //org.apache.commons.fileupload.servlet.ServletFileUpload; ServletFileUpload fileUpload = new ServletFileUpload(factory); // org.apache.commons.fileupload.FileItem; List<FileItem> list = null; try { list = fileUpload.parseRequest(request); for (FileItem fileItem : list) { if (fileItem.isFormField()) {//普通的字段 request.setAttribute(fileItem.getFieldName(), fileItem .getString()); } else {//是文件 String name = fileItem.getFieldName(); System.out.println(name); String value = fileItem.getName(); int start = value.lastIndexOf("//"); //不同的浏览器可能是不同的值,我们只取文件名 String filename = value.substring(start + 1); request.setAttribute(name, filename); fileItem .write(new File(request.getRealPath("/"), filename)); /*OutputStream out = new FileOutputStream(new File(request.getRealPath("/"),filename)); InputStream is =fileItem.getInputStream(); byte[] buff= new byte[1024]; int length=0; while((length=is.read(buff))>0){ out.write(buff, 0, length); } out.close(); is.close();*/ } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } request.getRequestDispatcher("result.jsp").forward(request, response); }
都是自己实现的,struts2中单个的文件可以这样获取 private File file;
private String fileFileName;//file-FileName固定的
private String fileContentType;//file-ContentType固定的 org.apache.struts2.interceptor.FileUploadInterceptor
InputStream is = new FileInputStream(file); String root = ServletActionContext.getRequest().getRealPath("/upload"); File fileto = new File(root,this.getFileFileName()); OutputStream out = new FileOutputStream(fileto); byte[] buff = new byte[1024]; int length=0; while((length=is.read(buff))>0){ out.write(buff, 0, length); } out.close(); is.close(); return SUCCESS;若对于多个文件则可以定义为 private List<File> file;
private List<String> fileFileName;
private List<String> fileContentType;存的时候循环遍历
for (int i=0;i<file.size();++i) { InputStream is = new FileInputStream(file.get(i)); String root = ServletActionContext.getRequest().getRealPath( "/upload"); File fileto = new File(root, this.getFileFileName().get(i)); OutputStream out = new FileOutputStream(fileto); byte[] buff = new byte[1024]; int length = 0; while ((length = is.read(buff)) > 0) { out.write(buff, 0, length); } out.close(); is.close(); }第15讲:接上一讲多个文件上传按钮:function addmore(){
var mo=document.getElementById("more");
var br = document.createElement("br");
var input = document.createElement("input");
var button= document.createElement("input");
input.type="file";
input.name="file";
button.type="button";
button.value="移除";
button.onclick=function(){
mo.removeChild(br);
mo.removeChild(input);
mo.removeChild(button);
}
mo.appendChild(br);
mo.appendChild(input);
mo.appendChild(button);
}看org.apache.struts2.interceptor.FileUploadInterceptor代码设置属性 protected static final Log log = LogFactory.getLog(FileUploadInterceptor.class);
private static final String DEFAULT_DELIMITER = ",";
private static final String DEFAULT_MESSAGE = "no.message.found"; protected Long maximumSize;//限制大小
protected String allowedTypes;//允许的文件类型
protected Set allowedTypesSet = Collections.EMPTY_SET;允许的文件类型参照tomcat下的conf下的web.xml如:<mime-mapping>
<extension>ppt</extension>
<mime-type>application/vnd.ms-powerpoint</mime-type>
</mime-mapping>则struts.xm:<interceptor-ref name="fileUpload">
<param name="maximumSize">20480000</param>
<param name="allowedTypes">application/vnd.ms-powerpoint</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>默认的defaultStack要设置错误的信息的key在代码中也有,用自己的信息,在message文件中替换就行了配置文件:
<action name="download" class="com.test.action.DownLoadAction"> <result name="success" type="stream"> <param name="contentType"> application/vnd.ms-powerpoint </param><!-- 下载文件的类型 --> <param name="contentDisposition">filename="test.ppt"</param><!-- 显示的文件名 --> <param name="inputName">downFile</param><!-- action中的get方法 --> </result> </action>action内容
import java.io.InputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class DownLoadAction extends ActionSupport { public InputStream getDownFile() { return ServletActionContext.getServletContext().getResourceAsStream( "/upload/CoreJava基础.ppt"); } @Override public String execute() throws Exception { return SUCCESS; } }会报这个错:java.net.SocketException: Connection reset by peer: socket write error大体搜了下。没有解释的好的。貌似是个bug
相关文章推荐
- BL_15 在线文件上传和下载 Struts 2.X + FileUpload
- Struts2学习笔记15:Struts2的文件上传和下载【续】二
- Python-Selenium2做Web自动化测试(12)-上传文件、下载文件、调用 JavaScript、控制浏览器滚动条
- struts2学习笔记(12)——文件上传与下载(2)
- JavaWEB——文件上传与下载
- JavaWeb学习总结(五十)——文件上传和下载
- 文件的上传和下载
- 利用SecureCRT上传、下载文件
- PHP---文件上传下载
- 在SecureCRT下使用sz下载和rz上传文件
- struts 实现文件上传与下载
- 文件上传下载所需工具类(文件上传、下载、打包下载、删除)
- Spring MVC 文件上传下载
- 在secureCRT下,使用rz和sz命令上传和下载文件
- javaweb 文件上传(fileupload) 下载
- C#文件上传和下载
- 在 mac 如何使用rz和sz上传和下载文件呢
- 基于sftp来上传和下载文件
- tornado 7、文件上传下载,验证码直接渲染到浏览器
- Openssh服务器的搭建,并使用PSFTP实现文件自动上传,下载的script命令