在用java实现文件上传下载过程中,
有高手可以分析下,
各种流的区别吗??
PrintWriter
FilterOutputStream
DataOutputStream
以及它们的相应输入流!!!
分别列举出,用它来实现 :
无格式文件(如,txt文件)上传
格式文件(如 doc文档)
或是图片,或是视频文件上传的优缺点???
以及,在代码实现过程中,哪些是最应该注意的!
有高手可以分析下,
各种流的区别吗??
PrintWriter
FilterOutputStream
DataOutputStream
以及它们的相应输入流!!!
分别列举出,用它来实现 :
无格式文件(如,txt文件)上传
格式文件(如 doc文档)
或是图片,或是视频文件上传的优缺点???
以及,在代码实现过程中,哪些是最应该注意的!
解决方案 »
- Java 对视频地址加密。
- 缺包
- 哪位大哥有spring+dwr安全校验解决策略或者源码
- Hibernate配置问题,急求高手解决,在线等,谢谢...
- 动态 actionForm 问题!在线等。。。。。
- 请高手帮忙看一下,谢谢
- J2EE/ Web/ EJB 问题: EJB Invocation failed on component 'xxx' ...
- 怎么设置一个隐藏的<html:text ...>标签
- 求一文件:jboss3.2.2下的javax.servlet.jar文件
- 请大家推荐一下学习j2ee和ejb的好书!!!
- java类中如何判断数据库中已经有了重复记录
- 跪求高手-----请教下谁知道fckeditor的一键排版功能
FilterOutputStream:Servlet中的字节输出流
DataOutputStream:基于Java类型的输出流
package com.servlet;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.bean.Product;
import com.dao.ProductOp;public class AddProductServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gb2312");
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
Product product = new Product();
product.setSerialNumber(request.getParameter("serialNumber"));
product.setName(request.getParameter("name"));
product.setBrand(request.getParameter("brand"));
product.setModel(request.getParameter("model"));
product.setPrice(Float.parseFloat(request.getParameter("price")));
String picture = request.getParameter("picture");//拿到路径
int index = picture.lastIndexOf("\\");
String path = picture.substring(index+1);//截取
product.setPicture(path);
product.setDescription(request.getParameter("description"));
ProductOp op = new ProductOp();
boolean flag = op.addProduct(product);
String str = "添加商品失败";
if(flag){
str = "添加商品成功";
//将图片存到image目录下
String realPath = this.getServletContext().getRealPath("/");
String imgPath = realPath + "image\\" + path;//服务器下图片的路径
FileInputStream fis = new FileInputStream(new File(picture));//从原始路径读取
FileOutputStream fos = new FileOutputStream(new File(imgPath));
byte[] byt = new byte[1024];
while(fis.read(byt) != -1){
fos.write(byt);
}
fos.close();
fis.close();
}
out.println("<script>alert('"+str+"')</script>");
response.setHeader("refresh", "0;url=admin/addProduct.jsp");
out.flush();
out.close();
}}
这是以前写过的一个servlet的上传
一个,你自己的相关java类没有给:ProductOp op = new ProductOp();
另一个,你的 String picture = request.getParameter("picture");//拿到路径
是通过什么方式进行传输过来的,通过 <input type = "file" .../>还是,通过 <input type="text".../> 传递过来的,没有说明。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bean;import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;/**
* realize the upload bean
* with a parameter which name is hid.
* @author Administrator
*/
public class upload implements Serializable { private String filesuffix; //-------文件的后缀名
private String fileName; //--------the complete name of a file but expect the path.
private String path; //---- without doubt, it is the physical location of a file
private String parameter_name;
private String parameter_value;
private boolean modifyFileStatus; //------用于标识文件名是否被更改
private boolean whetherCanTransfer; //---用于标志该文件是否可以进行传输,当其后缀名不是doc时,则不允许其进行传输。
private HttpServletRequest req;
private ServletInputStream input;
private byte[] contentByte = new byte[4096];
private int count = 0; public upload() {
filesuffix = "doc"; //---the document was default defined.
fileName = null;
path = "d:\\Excepriment"; parameter_name = "hid";
parameter_value = null;
this.modifyFileStatus = false;
this.whetherCanTransfer = false; //---默认不允许---
input = null;
req = null; } /**
* 默认,先传cid ,再传作业内容--
* @param req
*/
public void dealUpload() throws IOException {
HttpServletRequest request = this.getReq();
// request.setCharacterEncoding("utf-8"); ---- after experiment, this sentence exist or not will not affect the result.
input = request.getInputStream();
String s = "";
int haveread = 0;
//---取 hid 的值---
// boolean gethid =false;
while ((haveread = input.readLine(contentByte, 0, contentByte.length)) != -1) {
// System.out.println("####: "+contentByte.toString());
s = new String(contentByte, 0, haveread);
int begin = 0, last = 0;
if ((begin = s.indexOf("name=\"")) != -1 && (last = s.lastIndexOf("\"")) != -1) { String name = s.substring(begin + 6, last); // ---得到 string 是下标从 begin+6 到 last-1
// System.out.println("@@@@@: "+begin+"####: "+name);
if (this.parameter_name.equals(name)) // gethid = true ; //--传输流中,有带 hid 参数---
{
break;
}
}
}
if ((input.readLine(contentByte, 0, contentByte.length) != -1) &&
((haveread = input.readLine(contentByte, 0, contentByte.length)) != -1)) //------将空白行,去除;
{
s = new String(contentByte, 0, haveread); //--- haveread - 2同样得到正确结果--
int temp = s.lastIndexOf("\r\n");
if (temp != -1) {
s = s.substring(0, temp);
}
this.count = s.length();
this.parameter_value = s; }
//---------------到这里,以取得正确的隐藏参数值 hidden , value---------------
//--------取文件名-----------
while ((haveread = input.readLine(contentByte, 0, contentByte.length)) != -1) {
s = new String(contentByte, 0, haveread);
int begin = 0, last = 0;
if ((begin = s.indexOf("filename=\"")) != -1 && (last = s.lastIndexOf("\"")) != -1) {
String fullName = s.substring(begin + 10, last+1);
//---取后缀名---
int temp1 = fullName.lastIndexOf(".");
int temp2 = fullName.lastIndexOf("\"");
// fullName = fullName.toLowerCase();
// System.out.println("####temp1: "+temp1+" ####temp2: "+temp2+" ***####: "+fullName);
String tempSuffix = fullName.substring(temp1 + 1, temp2);
// System.out.println("####tempSuffix: "+tempSuffix);
int temp3 = fullName.lastIndexOf("\\");
String tempFull = fullName.substring(temp3, temp1); //----文件名 (不含后缀名)
if (Cantransfer(tempSuffix.toLowerCase(), tempFull)) {
Transfer();
break;
}
} } }
boolean successful = false;
HttpServletRequest re = this.getReq();
HttpSession s = re.getSession();
String sid;
if (this.filesuffix.equals(tempSuffix)) {
sid = (String) s.getAttribute("logid");
// sid = "1234567";
if (null != sid) {
this.fileName = sid + "_Exceperiment" + this.parameter_value; //----也就限定了,必须限定文件名---
if (this.fileName.equals(tempFull)); else { //--------如果没有按要求更改文件名,则自动为其更改---
this.setModifyFileStatus(true);
}
successful = true; }
this.setModifyFileStatus(true); //----文件后缀相同---,允许进行传输---
}
return successful;
} //---进行文件传输
public void Transfer() throws FileNotFoundException, IOException {
String filename = this.fileName + "." + this.filesuffix;
// File f = new File(this.path);
// if (!f.exists()) {
// f.mkdirs();
// }
String completeFileName = this.path + "\\" + filename;
// File newF = new File(completeFileName);
//----------到此,己成功创建好文件---接下来,准备数据,传输---
// PrintWriter output = new PrintWriter(newF);
FileOutputStream output = new FileOutputStream(completeFileName);
int haveread;
String s = "";
while ((haveread = input.readLine(contentByte, 0, contentByte.length)) != -1) {
s = new String(contentByte, 0, haveread);
int i = -1;
if ((i = s.indexOf("Content-Type")) != -1) {
break;
}
}
haveread = input.readLine(contentByte, 0, contentByte.length); //---------读掉一行无用数据--
//-----正式实现数据传输---
while ((haveread = input.readLine(contentByte, 0, contentByte.length)) != -1) {
s = new String(contentByte, 0, haveread);
if ((contentByte[0] == 45) && (contentByte[1] == 45) && (contentByte[2] == 45) && (contentByte[3] == 45) && (contentByte[4] == 45)) {
break;
}
System.out.println("Content: "+s);
output.write(contentByte, 0, haveread);
}
// output.flush();
output.close(); } /**
* @return the filesuffix
*/
public String getFilesuffix() {
return filesuffix;
} /**
* @param filesuffix the filesuffix to set
*/
public void setFilesuffix(String filesuffix) {
this.filesuffix = filesuffix;
} /**
* @return the fileName
*/
public String getFileName() {
return fileName;
} /**
* @param fileName the fileName to set
*/
public void setFileName(String fileName) {
this.fileName = fileName;
} /**
* @return the path
*/
public String getPath() {
return path;
} /**
* @param path the path to set
*/
public void setPath(String path) {
this.path = path;
} /**
* @return the input
*/
public ServletInputStream getInput() {
return input;
} /**
* @param input the input to set
*/
public void setInput(ServletInputStream input) {
this.input = input;
} /**
* @return the contentByte
*/
public byte[] getContentByte() {
return contentByte;
} /**
* @param contentByte the contentByte to set
*/
public void setContentByte(byte[] contentByte) {
this.contentByte = contentByte;
} /**
* @return the parameter_name
*/
public String getParameter_name() {
return parameter_name;
} /**
* @param parameter_name the parameter_name to set
*/
public void setParameter_name(String parameter_name) {
this.parameter_name = parameter_name;
} /**
* @return the parameter_value
*/
public String getParameter_value() {
return parameter_value;
} /**
* @param parameter_value the parameter_value to set
*/
public void setParameter_value(String parameter_value) {
this.parameter_value = parameter_value;
} /**
* @return the count
*/
public int getCount() {
return count;
} /**
* @param count the count to set
*/
public void setCount(int count) {
this.count = count;
} /**
* @return the req
*/
public HttpServletRequest getReq() {
return req;
} /**
* @param req the req to set
*/
public void setReq(HttpServletRequest req) {
this.req = req;
} /**
* @return the modifyFileStatus
*/
public boolean isModifyFileStatus() {
return modifyFileStatus;
} /**
* @param modifyFileStatus the modifyFileStatus to set
*/
public void setModifyFileStatus(boolean modifyFileStatus) {
this.modifyFileStatus = modifyFileStatus;
} /**
* @return the whetherCanTransfer
*/
public boolean isWhetherCanTransfer() {
return whetherCanTransfer;
} /**
* @param whetherCanTransfer the whetherCanTransfer to set
*/
public void setWhetherCanTransfer(boolean whetherCanTransfer) {
this.whetherCanTransfer = whetherCanTransfer;
}
// private
}