您好,欢迎来到尚车旅游网。
搜索
您的当前位置:首页解析含有enctype属性multipart-formdata的form表单并实现文件上传功能

解析含有enctype属性multipart-formdata的form表单并实现文件上传功能

来源:尚车旅游网


1.java代码:

package com.source;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.RandomAccessFile;

import javax.servlet.http.*;

public class UploadFile{

private String myFileName;

private HttpServletRequest request;

private HttpSession session;

public UploadFile(){}

//临时文件名;

private final String tmpName = String.valueOf(System.currentTimeMillis());;

/**

*获取request对象;

*/

public void setRequest(HttpServletRequest request){

this.request = request;

}

/**

*获取session对象;

*/

public void setSession(HttpSession session){

this.session = session;

}

public String getMyFileName(){

FileOutputStream out = null;

InputStream in = null;

RandomAccessFile readRandom = null;

RandomAccessFile writeRandom = null;

String fileName = \"\";

String filePath = \"\";

long start = 0;

long fileLength = 0;

try{

/**

*从request中获取输入流并把文件临时存入指定目录下;

*/

String path = \"d:\\\\aaaaaaaa\";

File dir = new File(path);

if(!dir.exists()){

dir.mkdirs();

}

File tmpFile = new File(dir,tmpName);

in = request.getInputStream();

out = new FileOutputStream(tmpFile);

byte[] bytes = new byte[1024];

int count =0;

while((count = in.read(bytes)) != -1){

out.write(bytes,0,count);

}

//关闭输入输出流;

in.close();

out.close();

/**

*读取上一步存入的文件并解析;

*/

readRandom = new RandomAccessFile(tmpFile,\"r\");

//获取文件第一行;

String content = readRandom.readLine();

String flag = \"\";

readRandom.seek(0);

while((flag = readRandom.readLine()) != null){

String str1 = \"\";

StringBuffer buffer1 = new StringBuffer();

while(((str1 = readRandom.readLine()) != null) && !(str1.isEmpty())){

if(!str1.equals(content)){

buffer1.append(str1);

}

}

String[] values = buffer1.toString().split(\";\");

/**

*把form其它参数存入map;

*/

for(String value:values){

/**

*获取上传文件的绝对路径;

*/

if(value.contains(\" filename=\")){

//获取文件绝对路径;

filePath = value.substring(11,value.lastIndexOf(\"\\\"\"));

//将绝对路径转为UTF-8编码;

filePath = new String(filePath.getBytes(\"ISO-8859-1\"),\"UTF-8\");

request.setAttribute(\"filePath\

//获取上传文件的起始位置;

start = readRandom.getFilePointer();

/**

*获取上传文件的长度fileLength;

*/

String c = \"\";

while((c = readRandom.readLine()) != null){

if(c.equals(content)){

fileLength = readRandom.getFilePointer() - content.length();

break;

}

if(c.equals(content+\"--\")){

fileLength = readRandom.getFilePointer() - (content+\"--\").length();

break;

}

}

fileLength = fileLength - start - 2;

//返回数据读取之前的原始位置

readRandom.seek(start);

String[] strs = buffer1.toString().split(\"\\\"\");

request.setAttribute(strs[1],strs[3]);

continue;

}

/**

*获取form页面参数;

*/

if(value.contains(\" name=\")){

//获取当前读取数据位置;

long now = readRandom.getFilePointer();

String v = \"\";

StringBuffer vb = new StringBuffer();

while((v = readRandom.readLine()) != null && !v.equals(content)

&& !v.equals(content+\"--\")){

vb.append(v);

}

//返回数据的原始位置;

readRandom.seek(now);

//把参数和值存入request,如果上传文件的参数已经存入,就不再存入;

if(request.getAttribute(value.substring(7,value.length()-1)) == null){

request.setAttribute(value.substring(7,value.length()-1),vb.toString());

}

}

}

}

/**

*从上传文件的绝对路径中获取上传文件名称;

*/

fileName = filePath.split(\"\\\\\\\\\")[filePath.split(\"\\\\\\\\\").length-1];

/**

*将上传的文件存入服务端;防止文件覆盖,重命名文件;

*/

String newFileName String.valueOf(System.currentTimeMillis()).concat(fileName);

File file2 = new File(dir,newFileName);

writeRandom = new RandomAccessFile(file2,\"rw\");

byte[] bs = new byte[10240];

int writeCount = 0;

/**

=

*从上传的数据中上传文件开始处读取;

*/

readRandom.seek(start);

while((writeCount = readRandom.read(bs)) != -1){

writeRandom.write(bs,0,writeCount);

}

/**

*设置保存在服务端的文件长度;

*/

writeRandom.setLength(fileLength-1);

//关闭输入输出流;

readRandom.close();

writeRandom.close();

//删除临时文件;

tmpFile.delete();

}catch(IOException e){

e.printStackTrace();

}finally{

try{

if(in != null){

in.close();

}

if(out != null){

out.close();

}

if(readRandom != null){

readRandom.close();

}

if(writeRandom != null){

writeRandom.close();

}

}catch(IOException e){

e.printStackTrace();

}

}

return fileName+\" upload successfully!\";

}

}2.jsp代码: (1)upload.jsp

<%@ page contentType=\"text/html;charset=UTF-8\"

pageEncoding=\"GBK\" %>

upload.jsp

姓名:

年龄:

性别:

个人简介:

选择文件:

(2)handle.jsp

<%@

page

contentType=\"text/html;charset=UTF-8\"

pageEncoding=\"GBK\" %>

<%

request.setCharacterEncoding(\"UTF-8\");

upload.setRequest(request);

upload.setSession(session);

upload.getMyFileName();

%>

handle.jsp

<%

String name = (String)request.getAttribute(\"name\");

name = new String(name.getBytes(\"ISO-8859-1\"),\"utf-8\");

String age = (String)request.getAttribute(\"age\");

age = new String(age.getBytes(\"ISO-8859-1\"),\"utf-8\");

String sex = (String)request.getAttribute(\"sex\");

sex = new String(sex.getBytes(\"ISO-8859-1\"),\"utf-8\");

String brief = (String)request.getAttribute(\"brief\");

brief = new String(brief.getBytes(\"ISO-8859-1\"),\"utf-8\");

%>

姓名:<%=name%>

年龄:<%=age%>

性别:<%=sex%>

个人简介:<%=brief%>

上传文件:<%=request.getAttribute(\"filePath\")%>

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- sceh.cn 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务