当前位置: 代码迷 >> Web前端 >> java servlet下传、上载
  详细解决方案

java servlet下传、上载

热度:179   发布时间:2012-09-20 09:36:50.0
java servlet上传、下载

?

<!--文件上传 servlet-->

public class UpFile extends HttpServlet{

?

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=utf-8");

response.setCharacterEncoding("utf-8");

DiskFileItemFactory factory = new DiskFileItemFactory();?

factory.setSizeThreshold(1024 * 1024 * 20);

//factory.setRepository(new File(request.getRealPath("/")));

ServletFileUpload upload = new ServletFileUpload(factory);

// 允许上传的文件类型?

//upload.setAllowedFilesList("doc,xls,");?

? // 拒绝上传的文件类型?

//upload.setDeniedFilesList("exe,bat,jsp");?

//文件大小20M

upload.setSizeMax(1024 * 1024 * 20);

?

List items;

String affix =null;

try {

items = upload.parseRequest(request);

for (Iterator it = items.iterator(); it.hasNext();) {

FileItem item = (FileItem) it.next();

if (item.isFormField()) {

String name = item.getFieldName();

?

if(name.equals("boy"))

{

affix = item.getString("utf-8");

}

} else {

if (item.getSize() > 0) {

String fieldName = item.getFieldName();

String fileName =item.getName();

String contentType = item.getContentType();

/*affix = System.currentTimeMillis()

+ fileName.substring(fileName.lastIndexOf("."),

fileName.length());*/

//affix = fileName.substring(fileName.lastIndexOf("."),fileName.length());

fileName = new String(fileName.getBytes("utf-8"),"utf-8");?

affix = fileName;

//设置上传路径

FileOutputStream fos = new FileOutputStream(request.getRealPath("Upload")+"/"+ affix);

if (item.isInMemory()) {

fos.write(item.get());

}

else {

InputStream is = item.getInputStream();

byte[] buffer = new byte[1024];

int len;

while ((len = is.read(buffer)) > 0) {

fos.write(buffer, 0, len);

}

is.close();

}

fos.flush();

fos.close();

}

}

}

} catch (FileUploadException e) {

e.printStackTrace();

}

?

}

}

<!--结束 -->

<!-- 文件下载servlet-->

?

public class DownFile extends HttpServlet {

private ServletConfig config; ?

protected void ?doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

java.io.BufferedInputStream bis = null;

java.io.BufferedOutputStream bos = null;

try {

String filename = request.getParameter("filename");

filename = new String(filename.getBytes("iso-8859-1"), "utf-8");

response.setContentType("application/x-msdownload");

response.setHeader("Content-disposition",

"attachment; filename="+ new String(filename.getBytes("iso-8859-1"),"utf-8"));

//设置下载路径

bis = new java.io.BufferedInputStream(

new java.io.FileInputStream(config.getServletContext().getRealPath("Upload/" + filename)));

bos = new java.io.BufferedOutputStream(response

.getOutputStream());

byte[] buff = new byte[2048];

int bytesRead;

while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {

bos.write(buff, 0, bytesRead);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

if (bis != null)

bis.close();

if (bos != null)

bos.close();

}

}

public void init(ServletConfig config) throws ServletException {

this.config = config;

}

}

<-- 下载结束-->

<-- 对请求信息过滤-->

?

public class Filte implements Filter{

?

public void destroy() {

}

?

public void doFilter(ServletRequest request, ServletResponse response,

FilterChain chain) throws IOException, ServletException {

HttpServletRequest hRequest = (HttpServletRequest)request;

HttpServletResponse hResponse = (HttpServletResponse)response;

hRequest.setCharacterEncoding("UTF-8");

hResponse.setCharacterEncoding("UTF-8");

chain.doFilter(hRequest, hResponse);

}

?

public void init(FilterConfig arg0) throws ServletException {

?

}

?

}

<-- 过滤结束-->

<!-- jsp页面开始-->

?

? <body>

?

<P>选择要上传的文件:<BR>

? ?<FORM action="<%=basePath %>UpFile" method="post" ENCTYPE="multipart/form-data">

? ? ? <INPUT type=FILE name="boy" size="45">?

? ? ? <BR> <INPUT type="submit" name ="g" value="提交">

? ?</FORM>

?

? ? ?<!-- 文件名应该为查询出来的,在这里先使用刚上传的文件名 -->

? ?<%?

String fname ="mark.txt";

? ? ?%>

?

? 支持目标另存为

?<A target="_blank" href="<%=basePath%>DownFile?filename=<%=fname%>">下 载</A>

?

? </body>

<!--jsp页面结束 -->

?

?

注:配置一下web.xml

1 楼 sha851092391 2011-11-21  
LZ你试一下中文的文件名字,用多个浏览器测试一下上传,文件名是不是乱码了。
2 楼 miaowei 2011-11-21  
sha851092391 写道
LZ你试一下中文的文件名字,用多个浏览器测试一下上传,文件名是不是乱码了。

你好,通过对IE,Google,Firefox三个浏览器上传测试没有发现中文乱码,不过在下载时点击另存为---出现的文件名倒是乱码。这个问题我也一直在找。
  相关解决方案