java中的接口是类吗
185
2023-07-09
JavaWeb文件上传开发实例
文件上传有SmartUpload、Apache的Commons fileupload.我们今天介绍Commons fileupload的用法。
1.commons-fileupload-1.3.1.jar
下载地址:http://xiazai.jb51.net/201608/yuanma/javawebfileupload(jb51.net).rar
2.web.xml
3.UploadFile.jsp
3.servlet
package com.servlet;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import sun.swing.FilePane;
public class UploadServlet extends HttpServlet {
private String filePath;
/**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.getWriter().println("请以POST方式上传文件");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
File file1 = null, file2 = null;
String description1 = null, description2 = null;
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
DiskFileUpload diskFileUpload = new DiskFileUpload();//解析request
try {
List
out.println("遍历所有的FileItem...");
this.log("遍历所有的FileItem...");
for(FileItem fileItem : fileList){
if(fileItem.isFormField()){//如果是文本域
if("description1".equals(fileItem.getFieldName())){
out.println("遍历到description1...");
description1 = new String(fileItem.getString().getBytes(),"UTF-8");
}
if("description2".equals(fileItem.getFieldName())){
out.println("遍历到description2...");
description2 = new String(fileItem.getString().getBytes(),"UTF-8");
}
}else{//否则为文件域
if("file1".equals(fileItem.getFieldName())){
File remoteFile = new File(new String(fileItem.getName().getBytes(),"UTF-8"));
out.println("遍历到file1...");
out.println("客户端文件位置:"+remoteFile.getAbsolutePath()+"");
//服务器端文件,放在uploadfile文件夹下
file1 = new File(filePath,remoteFile.getName());
file1.getParentFile().mkdirs();//创建文件夹路径
file1.createNewFile();//创建新文件
InputStream ins = fileItem.getInputStream();//FileItem的内容
OutputStream ous = new FileOutputStream(file1);//输出到文件中
try{
byte[] buffer = new byte[1024];//字节缓存
int len = 0;//实际缓存长度
while((len=ins.read(buffer)) > -1){//循环读入缓存
ous.write(buffer,0,len);
}
out.println("已保存文件"+file1.getAbsolutePath()+"");
}
finally{
ous.close();
ins.close();
}
}
if("file2".equals(fileItem.getFieldName())){
File remoteFile = new File(new String(fileItem.getName().getBytes(),"UTF-8"));
out.println("遍历到file2...");
out.println("客户端文件位置:"+remoteFile.getAbsolutePath()+"");
//服务器端文件,放在uploadfile文件夹下
file2 = new File(filePath,remoteFile.getName());
file2.getParentFile().mkdirs();//创建文件夹路径
file2.createNewFile();//创建新文件
InputStream ins = fileItem.getInputStream();//FileItem的内容
OutputStream ous = new FileOutputStream(file2);//输出到文件中
try{
byte[] buffer = new byte[1024];//字节缓存
int len = 0;//实际缓存长度
while((len=ins.read(buffer)) > -1){//循环读入缓存
ous.write(buffer,0,len);
}
out.println("已保存文件"+file2.getAbsolutePath()+"");
}
finally{
ous.close();
ins.close();
}
}
}
}
out.println("Request 解析完毕");
} catch (FileUploadException e) {
// TODO 自动生成的 catch 块
out.println(" ");
out.println("");
out.println("
out.println("
if(file1 != null){
out.println("
out.println("
out.println("
out.println("
out.println("
}
if(file2 != null){
out.println("
out.println("
out.println("
out.println("
out.println("
}
out.println("
out.println("
out.println("
out.println(description1);
out.println("
out.println("
out.println("
out.println("
out.println("
out.println(description2);
out.println("
out.println("
out.println(" ");
out.println("");
out.flush();
out.close();
}
}
public String getServletInfo() {
return "This is my default servlet created by Eclipse";
}
public void init() throws ServletException {
// Put your code here
filePath = getServletConfig().getInitParameterJCsEhy("filePath");
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~